poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] libpoke,pickles: Ban anonymous members in unions.


From: David Faust
Subject: [PATCH] libpoke,pickles: Ban anonymous members in unions.
Date: Mon, 8 Feb 2021 12:25:11 -0800

At the Poke level, unions consist of only one member with named
alternates; there is no other way to refer to union members except by
name. This leads to several issues with allowing anonymous union members.

The PVM code generated by struct_printer visits each member in a union
by name, which leads to a segmentation fault when attempting to compile
the printer for unions with anonymous members.

A minimal reproducer:
  (poke) type Foo = union { int; };
  (poke) printf "%v", Foo {};

A second (related) issue is that using unions with anonymous members is
highly problematic. If a union value is created by selecting an
anonymous member, it is a black box. If a union contains multiple
anonymous members, there is no way to distinguish between them.

After discussion, it seems the most sensible solution to these problems
is to forbid anonymous members in unions entirely.

2021-02-08  David Faust  <david.faust@oracle.com>

        * libbpoke/pkl-anal.c: Forbid anonymous members in unions.
        * pickles/btf.pk (type BTF_Type): Give name to anonymous union member.
        * pickles/ctf.pk (type CTF_Type): Likewise.
        * pickles/dwarf-expr.pk (type Dwarf_Op): Likewise.
        * pickles/dwarf-frame.pk (type Dwarf_CFI): Likewise.
        * testsuite/poke.pkl/union-diag-8.pk: New test.
        * testsuite/Makefile.am (EXTRA_DIST): Update.
---
 ChangeLog                          | 10 ++++++++++
 libpoke/pkl-anal.c                 | 17 +++++++++++++----
 pickles/btf.pk                     |  2 +-
 pickles/ctf.pk                     |  2 +-
 pickles/dwarf-expr.pk              |  2 +-
 pickles/dwarf-frame.pk             |  2 +-
 testsuite/Makefile.am              |  1 +
 testsuite/poke.pkl/union-diag-8.pk |  9 +++++++++
 8 files changed, 37 insertions(+), 8 deletions(-)
 create mode 100644 testsuite/poke.pkl/union-diag-8.pk

diff --git a/ChangeLog b/ChangeLog
index e2caeef1..b1af9646 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2021-02-08  David Faust  <david.faust@oracle.com>
+
+       * libbpoke/pkl-anal.c: Forbid anonymous members in unions.
+       * pickles/btf.pk (type BTF_Type): Give name to anonymous union member.
+       * pickles/ctf.pk (type CTF_Type): Likewise.
+       * pickles/dwarf-expr.pk (type Dwarf_Op): Likewise.
+       * pickles/dwarf-frame.pk (type Dwarf_CFI): Likewise.
+       * testsuite/poke.pkl/union-diag-8.pk: New test.
+       * testsuite/Makefile.am (EXTRA_DIST): Update.
+
 2021-02-06  Jose E. Marchesi  <jemarch@gnu.org>
 
        * poke/pk-cmd-ios.c (pk_cmd_source_file): New function.
diff --git a/libpoke/pkl-anal.c b/libpoke/pkl-anal.c
index a6d8e18f..cd4046d4 100644
--- a/libpoke/pkl-anal.c
+++ b/libpoke/pkl-anal.c
@@ -195,13 +195,22 @@ PKL_PHASE_BEGIN_HANDLER (pkl_anal1_ps_type_struct)
 
   for (t = struct_type_elems; t; t = PKL_AST_CHAIN (t))
     {
+      pkl_ast_node tname
+        = (PKL_AST_CODE (t) == PKL_AST_STRUCT_TYPE_FIELD
+           ? PKL_AST_STRUCT_TYPE_FIELD_NAME (t)
+           : PKL_AST_DECL_NAME (t));
       pkl_ast_node u;
+
+      if (PKL_AST_TYPE_S_UNION_P (struct_type) && !tname)
+        {
+          PKL_ERROR (PKL_AST_LOC (t),
+                     "anonymous members are not allowed in unions");
+          PKL_ANAL_PAYLOAD->errors++;
+          PKL_PASS_ERROR;
+        }
+
       for (u = struct_type_elems; u != t; u = PKL_AST_CHAIN (u))
         {
-          pkl_ast_node tname
-            = (PKL_AST_CODE (t) == PKL_AST_STRUCT_TYPE_FIELD
-               ? PKL_AST_STRUCT_TYPE_FIELD_NAME (t)
-               : PKL_AST_DECL_NAME (t));
           pkl_ast_node uname
             = (PKL_AST_CODE (u) == PKL_AST_STRUCT_TYPE_FIELD
                ? PKL_AST_STRUCT_TYPE_FIELD_NAME (u)
diff --git a/pickles/btf.pk b/pickles/btf.pk
index adc3c690..ccf85866 100644
--- a/pickles/btf.pk
+++ b/pickles/btf.pk
@@ -164,7 +164,7 @@ type BTF_Type =
       BTF_Member[info.vlen] members      : info.kind == BTF_KIND_UNION;
       BTF_Var_SecInfo[info.vlen] datasec : info.kind == BTF_KIND_DATASEC;
 
-      struct {};
+      struct {} nothing;
     } data;
 
     method vararg_p = int:
diff --git a/pickles/ctf.pk b/pickles/ctf.pk
index 8f9a22f8..37fa2118 100644
--- a/pickles/ctf.pk
+++ b/pickles/ctf.pk
@@ -260,7 +260,7 @@ type CTF_Type =
       CTF_Member[info.vlen] members    : info.kind in 
[CTF_KIND_STRUCT,CTF_KIND_UNION];
       CTF_Enum enum                    : info.kind == CTF_KIND_ENUM;
       CTF_Type_Id[info.vlen] arg_types : info.kind == CTF_KIND_FUNCTION;
-      struct {};
+      struct {} nothing;
     } data;
   };
 
diff --git a/pickles/dwarf-expr.pk b/pickles/dwarf-expr.pk
index ddbfd3cf..ab5d9e2b 100644
--- a/pickles/dwarf-expr.pk
+++ b/pickles/dwarf-expr.pk
@@ -248,6 +248,6 @@ type Dwarf_Op =
       Dwarf_Section_Offset offset : code == DW_OP_call_ref;
 
       /* Other operations have no arguments.  */
-      struct {};
+      struct {} nothing;
     } arg;
   };
diff --git a/pickles/dwarf-frame.pk b/pickles/dwarf-frame.pk
index 0cee3b79..b64e52c4 100644
--- a/pickles/dwarf-frame.pk
+++ b/pickles/dwarf-frame.pk
@@ -120,7 +120,7 @@ type Dwarf_CFI =
                                   DW_CFA_val_expression];
 
       /* Other instructions have no operands. */
-      struct {};
+      struct {} nothing;
     } ops;
   };
 
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 9c10cdbb..2f1c27cb 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -1775,6 +1775,7 @@ EXTRA_DIST = \
   poke.pkl/union-diag-5.pk \
   poke.pkl/union-diag-6.pk \
   poke.pkl/union-diag-7.pk \
+  poke.pkl/union-diag-8.pk \
   poke.pkl/union-pretty-print-1.pk \
   poke.pkl/units-1.pk \
   poke.pkl/units-2.pk \
diff --git a/testsuite/poke.pkl/union-diag-8.pk 
b/testsuite/poke.pkl/union-diag-8.pk
new file mode 100644
index 00000000..4fab78ac
--- /dev/null
+++ b/testsuite/poke.pkl/union-diag-8.pk
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+
+/* Check for anonymous members in unions.  */
+
+type Foo = union
+  {
+    int a;
+    int; /* { dg-error "" } */
+  };
-- 
2.30.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]