[3/3] bpf: set index entry for a VAR_DECL in CO-RE relocs

Message ID 20240927164917.141266-4-cupertino.miranda@oracle.com
State New
Headers
Series bpf: CO-RE fixes |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 success Test passed

Commit Message

Cupertino Miranda Sept. 27, 2024, 4:49 p.m. UTC
  CO-RE accesses with non pointer struct variables will also generate a
"0" string access within the CO-RE relocation.
The first index within the access string, has sort of a different
meaning then the remaining of the indexes.
For i0:i1:...:in being an access index for "struct A a" declaration, its
semantics are represented by:
  (&a + (sizeof(struct A) * i0) + offsetof(i1:...:in)
---
 gcc/config/bpf/core-builtins.cc                  |  5 ++++-
 gcc/testsuite/gcc.target/bpf/core-builtin-1.c    | 16 ++++++++--------
 gcc/testsuite/gcc.target/bpf/core-builtin-2.c    |  3 ++-
 .../gcc.target/bpf/core-builtin-exprlist-1.c     | 16 ++++++++--------
 4 files changed, 22 insertions(+), 18 deletions(-)
  

Comments

David Faust Sept. 30, 2024, 5:47 p.m. UTC | #1
On 9/27/24 09:49, Cupertino Miranda wrote:
> CO-RE accesses with non pointer struct variables will also generate a
> "0" string access within the CO-RE relocation.
> The first index within the access string, has sort of a different
> meaning then the remaining of the indexes.
> For i0:i1:...:in being an access index for "struct A a" declaration, its
> semantics are represented by:
>   (&a + (sizeof(struct A) * i0) + offsetof(i1:...:in)

I can guess the answer, but is this semantic actually documented anywhere?

We may want to see about adding this in the "official" kernel BTF docs
since IMO this special meaning of the first index is not at all obvious.

Patch LGTM.
Thanks.

> ---
>  gcc/config/bpf/core-builtins.cc                  |  5 ++++-
>  gcc/testsuite/gcc.target/bpf/core-builtin-1.c    | 16 ++++++++--------
>  gcc/testsuite/gcc.target/bpf/core-builtin-2.c    |  3 ++-
>  .../gcc.target/bpf/core-builtin-exprlist-1.c     | 16 ++++++++--------
>  4 files changed, 22 insertions(+), 18 deletions(-)
> 
> diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
> index cdfb356660e..fc6379cf028 100644
> --- a/gcc/config/bpf/core-builtins.cc
> +++ b/gcc/config/bpf/core-builtins.cc
> @@ -698,10 +698,13 @@ compute_field_expr (tree node, unsigned int *accessors,
>  			      access_node, false, callback);
>        return n;
>  
> +    case VAR_DECL:
> +      accessors[0] = 0;
> +      return 1;
> +
>      case ADDR_EXPR:
>      case CALL_EXPR:
>      case SSA_NAME:
> -    case VAR_DECL:
>      case PARM_DECL:
>        return 0;
>      default:
> diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-1.c b/gcc/testsuite/gcc.target/bpf/core-builtin-1.c
> index b4f9998afb8..0706005f0e5 100644
> --- a/gcc/testsuite/gcc.target/bpf/core-builtin-1.c
> +++ b/gcc/testsuite/gcc.target/bpf/core-builtin-1.c
> @@ -24,16 +24,16 @@ unsigned long ula[8];
>  unsigned long
>  func (void)
>  {
> -  /* 1 */
> +  /* 0:1 */
>    int b = _(my_s.b);
>  
> -  /* 2 */
> +  /* 0:2 */
>    char c = _(my_s.c);
>  
> -  /* 2:3 */
> +  /* 0:2:3 */
>    unsigned char uc = _(my_u.uc[3]);
>  
> -  /* 6 */
> +  /* 0:6 */
>    unsigned long ul = _(ula[6]);
>  
>    return b + c + uc + ul;
> @@ -55,10 +55,10 @@ u_ptr (union U *pu)
>    return x;
>  }
>  
> -/* { dg-final { scan-assembler-times "ascii \"1.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> -/* { dg-final { scan-assembler-times "ascii \"2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> -/* { dg-final { scan-assembler-times "ascii \"2:3.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> -/* { dg-final { scan-assembler-times "ascii \"6.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> +/* { dg-final { scan-assembler-times "ascii \"0:1.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> +/* { dg-final { scan-assembler-times "ascii \"0:2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> +/* { dg-final { scan-assembler-times "ascii \"0:2:3.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> +/* { dg-final { scan-assembler-times "ascii \"0:6.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
>  /* { dg-final { scan-assembler-times "ascii \"0:2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
>  /* { dg-final { scan-assembler-times "ascii \"0:2:3.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
>  
> diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-2.c b/gcc/testsuite/gcc.target/bpf/core-builtin-2.c
> index b72e2566b71..04b3f6b2652 100644
> --- a/gcc/testsuite/gcc.target/bpf/core-builtin-2.c
> +++ b/gcc/testsuite/gcc.target/bpf/core-builtin-2.c
> @@ -16,11 +16,12 @@ struct S foo;
>  
>  void func (void)
>  {
> +  /* 0:1:3:2 */
>    char *x = __builtin_preserve_access_index (&foo.u[3].c);
>  
>    *x = 's';
>  }
>  
>  /* { dg-final { scan-assembler-times "\[\t \]0x4000002\[\t \]+\[^\n\]*btt_info" 1 } } */
> -/* { dg-final { scan-assembler-times "ascii \"1:3:2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> +/* { dg-final { scan-assembler-times "ascii \"0:1:3:2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
>  /* { dg-final { scan-assembler-times "bpfcr_type" 1 } } */
> diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-exprlist-1.c b/gcc/testsuite/gcc.target/bpf/core-builtin-exprlist-1.c
> index 8ce4a6e70de..c53daf81c5f 100644
> --- a/gcc/testsuite/gcc.target/bpf/core-builtin-exprlist-1.c
> +++ b/gcc/testsuite/gcc.target/bpf/core-builtin-exprlist-1.c
> @@ -31,16 +31,16 @@ func (void)
>    int ic;
>  
>    __builtin_preserve_access_index (({
> -    /* 1 */
> +    /* 0:1 */
>      b = my_s.b;
>  
> -    /* 2 */
> +    /* 0:2 */
>      ic = my_s.c;
>  
> -    /* 2:3 */
> +    /* 0:2:3 */
>      uc = my_u.uc[3];
>  
> -    /* 6 */
> +    /* 0:6 */
>      ul = ula[6];
>    }));
>  
> @@ -65,10 +65,10 @@ u_ptr (union U *pu)
>    return x;
>  }
>  
> -/* { dg-final { scan-assembler-times "ascii \"1.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> -/* { dg-final { scan-assembler-times "ascii \"2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> -/* { dg-final { scan-assembler-times "ascii \"2:3.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> -/* { dg-final { scan-assembler-times "ascii \"6.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> +/* { dg-final { scan-assembler-times "ascii \"0:1.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> +/* { dg-final { scan-assembler-times "ascii \"0:2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> +/* { dg-final { scan-assembler-times "ascii \"0:2:3.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
> +/* { dg-final { scan-assembler-times "ascii \"0:6.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
>  /* { dg-final { scan-assembler-times "ascii \"0:2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
>  /* { dg-final { scan-assembler-times "ascii \"0:2:3.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
>
  

Patch

diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index cdfb356660e..fc6379cf028 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -698,10 +698,13 @@  compute_field_expr (tree node, unsigned int *accessors,
 			      access_node, false, callback);
       return n;
 
+    case VAR_DECL:
+      accessors[0] = 0;
+      return 1;
+
     case ADDR_EXPR:
     case CALL_EXPR:
     case SSA_NAME:
-    case VAR_DECL:
     case PARM_DECL:
       return 0;
     default:
diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-1.c b/gcc/testsuite/gcc.target/bpf/core-builtin-1.c
index b4f9998afb8..0706005f0e5 100644
--- a/gcc/testsuite/gcc.target/bpf/core-builtin-1.c
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-1.c
@@ -24,16 +24,16 @@  unsigned long ula[8];
 unsigned long
 func (void)
 {
-  /* 1 */
+  /* 0:1 */
   int b = _(my_s.b);
 
-  /* 2 */
+  /* 0:2 */
   char c = _(my_s.c);
 
-  /* 2:3 */
+  /* 0:2:3 */
   unsigned char uc = _(my_u.uc[3]);
 
-  /* 6 */
+  /* 0:6 */
   unsigned long ul = _(ula[6]);
 
   return b + c + uc + ul;
@@ -55,10 +55,10 @@  u_ptr (union U *pu)
   return x;
 }
 
-/* { dg-final { scan-assembler-times "ascii \"1.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
-/* { dg-final { scan-assembler-times "ascii \"2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
-/* { dg-final { scan-assembler-times "ascii \"2:3.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
-/* { dg-final { scan-assembler-times "ascii \"6.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
+/* { dg-final { scan-assembler-times "ascii \"0:1.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
+/* { dg-final { scan-assembler-times "ascii \"0:2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
+/* { dg-final { scan-assembler-times "ascii \"0:2:3.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
+/* { dg-final { scan-assembler-times "ascii \"0:6.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
 /* { dg-final { scan-assembler-times "ascii \"0:2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
 /* { dg-final { scan-assembler-times "ascii \"0:2:3.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
 
diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-2.c b/gcc/testsuite/gcc.target/bpf/core-builtin-2.c
index b72e2566b71..04b3f6b2652 100644
--- a/gcc/testsuite/gcc.target/bpf/core-builtin-2.c
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-2.c
@@ -16,11 +16,12 @@  struct S foo;
 
 void func (void)
 {
+  /* 0:1:3:2 */
   char *x = __builtin_preserve_access_index (&foo.u[3].c);
 
   *x = 's';
 }
 
 /* { dg-final { scan-assembler-times "\[\t \]0x4000002\[\t \]+\[^\n\]*btt_info" 1 } } */
-/* { dg-final { scan-assembler-times "ascii \"1:3:2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
+/* { dg-final { scan-assembler-times "ascii \"0:1:3:2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
 /* { dg-final { scan-assembler-times "bpfcr_type" 1 } } */
diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-exprlist-1.c b/gcc/testsuite/gcc.target/bpf/core-builtin-exprlist-1.c
index 8ce4a6e70de..c53daf81c5f 100644
--- a/gcc/testsuite/gcc.target/bpf/core-builtin-exprlist-1.c
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-exprlist-1.c
@@ -31,16 +31,16 @@  func (void)
   int ic;
 
   __builtin_preserve_access_index (({
-    /* 1 */
+    /* 0:1 */
     b = my_s.b;
 
-    /* 2 */
+    /* 0:2 */
     ic = my_s.c;
 
-    /* 2:3 */
+    /* 0:2:3 */
     uc = my_u.uc[3];
 
-    /* 6 */
+    /* 0:6 */
     ul = ula[6];
   }));
 
@@ -65,10 +65,10 @@  u_ptr (union U *pu)
   return x;
 }
 
-/* { dg-final { scan-assembler-times "ascii \"1.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
-/* { dg-final { scan-assembler-times "ascii \"2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
-/* { dg-final { scan-assembler-times "ascii \"2:3.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
-/* { dg-final { scan-assembler-times "ascii \"6.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
+/* { dg-final { scan-assembler-times "ascii \"0:1.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
+/* { dg-final { scan-assembler-times "ascii \"0:2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
+/* { dg-final { scan-assembler-times "ascii \"0:2:3.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
+/* { dg-final { scan-assembler-times "ascii \"0:6.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
 /* { dg-final { scan-assembler-times "ascii \"0:2.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */
 /* { dg-final { scan-assembler-times "ascii \"0:2:3.0\"\[\t \]+\[^\n\]*btf_aux_string" 1 } } */