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

clang_parser: workaround for asm goto in Kernel 5+ headers #732

Merged
merged 1 commit into from
Jun 17, 2019

Conversation

mmarchini
Copy link
Contributor

Fixes: #611

@mmarchini
Copy link
Contributor Author

I couldn't test it yet because I don't have a Kernel 5+ readily available now.

@joestringer @olsajiri if you could try it that would be really helpful, since you both were suffering from this issue.

@mmarchini
Copy link
Contributor Author

Also, I'll need to update codegen and parser tests later.

@mmarchini mmarchini added the do-not-merge Changes are not ready to be merged into master yet label Jun 7, 2019
@olsajiri
Copy link
Contributor

olsajiri commented Jun 7, 2019

I couldn't test it yet because I don't have a Kernel 5+ readily available now.

@joestringer @olsajiri if you could try it that would be really helpful, since you both were suffering from this issue.

with the extra change below, your fix is working for me,
we need to include linux/types.h to load the compiler headers

I wonder we should put it to extra unsaved_files header,
like they did in samples/bpf/asm_goto_workaround.h

diff --git a/src/clang_parser.cpp b/src/clang_parser.cpp
index 509d01453032..98e788c7ad49 100644
--- a/src/clang_parser.cpp
+++ b/src/clang_parser.cpp
@@ -294,7 +294,8 @@ bool ClangParser::parse(ast::Program *program, BPFtrace &bpftrace, std::vector<s
   // able to parse our headers.
   //
   // From: https://github.com/iovisor/bcc/pull/2133/files
-  input = "#ifdef asm_volatile_goto\n"
+  input = "#include <linux/types.h>\n"
+          "#ifdef asm_volatile_goto\n"
           "#undef asm_volatile_goto\n"
           "#define asm_volatile_goto(x...) asm volatile(\"invalid use of asm_volatile_goto\")\n"
           "#endif\n\n" + input;

@mmarchini
Copy link
Contributor Author

we need to include linux/types.h to load the compiler headers

It is already included (or at least should be, unless something changed recently).

@olsajiri
Copy link
Contributor

olsajiri commented Jun 7, 2019

yes, but I think it needs to come before the #undef, so it cant get disabled

@mmarchini
Copy link
Contributor Author

I was looking at bcc's implementation. After looking at samples/bpf/asm_goto_workaround.h I agree it would be safer if the macro came after including types.h. What about adding our workaround after the C definitions input?

diff --git a/src/clang_parser.cpp b/src/clang_parser.cpp
index 509d014..9498a9c 100644
--- a/src/clang_parser.cpp
+++ b/src/clang_parser.cpp
@@ -294,10 +294,10 @@ bool ClangParser::parse(ast::Program *program, BPFtrace &bpftrace, std::vector<s
   // able to parse our headers.
   //
   // From: https://github.com/iovisor/bcc/pull/2133/files
-  input = "#ifdef asm_volatile_goto\n"
+  input = input + "#ifdef asm_volatile_goto\n"
           "#undef asm_volatile_goto\n"
           "#define asm_volatile_goto(x...) asm volatile(\"invalid use of asm_volatile_goto\")\n"
-          "#endif\n\n" + input;
+          "#endif\n\n";
 
   CXUnsavedFile unsaved_files[] =
   {

This way we guarantee that nothing inside the program will override our definiton (and as a plus we don't need to change our tests :) ).

@olsajiri
Copy link
Contributor

that still fails for me with the same errors,
I pushed out temporary fix which I'm using at the moment
for reference: olsajiri@641162a

@mmarchini mmarchini removed the do-not-merge Changes are not ready to be merged into master yet label Jun 11, 2019
@mmarchini
Copy link
Contributor Author

@olsajiri thank you for sharing your fix. I made a few changes and updated the PR, should be working now.

@@ -3,6 +3,9 @@
#include <clang-c/Index.h>
#include "bpftrace.h"

#define ASM_GOTO_WORKAROUND_H "asm_goto_workaround.h"
#define ASM_GOTO_WORKAROUND_PATH "/bpftrace/include/" ASM_GOTO_WORKAROUND_H
Copy link
Member

Choose a reason for hiding this comment

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

Should normally wrap the contents of a #define in brackets for preprocessor safety, but since ASM_GOTO_WORKAROUND_PATH is only used once anyway we might as well just get rid of this define. That would match how the other builtin headers are done too.

@@ -321,6 +321,11 @@ bool ClangParser::parse(ast::Program *program, BPFtrace &bpftrace, std::vector<s
.Contents = stdint_h,
.Length = stdint_h_len,
},
{
.Filename = ASM_GOTO_WORKAROUND_PATH,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.Filename = ASM_GOTO_WORKAROUND_PATH,
.Filename = "/bpftrace/include/" ASM_GOTO_WORKAROUND_H,

To try and clarify what I was talking about in the other comment

src/clang_parser.h Show resolved Hide resolved
@joestringer
Copy link

Got around to testing this out, it works beautifully thanks folks!

viktormalik added a commit to viktormalik/bpftrace that referenced this pull request Jan 3, 2023
<linux/types.h> is included from clang_workarounds.h which contains a
workaround for asm goto which is not supported by older Clang/LLVM
versions. The problem is that types from <linux/types.h> conflict with
types from <sys/types.h> which may cause type redefinitions in Clang
parser. This particularly happens when BTF is not present b/c otherwise
<linux/types.h> and clang_workarounds.h are not used.

This commit drops the above include. This should be safe to do as the
workaround will still work if <linux/types.h> is included *before*
including clang_workarounds.h (see [1] for more details).

[1] bpftrace#732
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.

tcpaccept failed with "terminate called after throwing an instance of 'std::out_of_range'"
4 participants