jit: Add checking for dereference of void *
authorDavid Malcolm <dmalcolm@redhat.com>
Thu, 8 Jan 2015 01:08:19 +0000 (01:08 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Thu, 8 Jan 2015 01:08:19 +0000 (01:08 +0000)
gcc/jit/ChangeLog:
* jit-recording.h (gcc::jit::recording::type::is_void): New
virtual function.
(gcc::jit::recording::memento_of_get_type::is_void): New
function, overriding default implementation.
* libgccjit.c (gcc_jit_rvalue_dereference): Verify that
the underlying type is not "void".

gcc/testsuite/ChangeLog:
* jit.dg/test-error-dereferencing-void-ptr.c: New test case.

From-SVN: r219333

gcc/jit/ChangeLog
gcc/jit/jit-recording.h
gcc/jit/libgccjit.c
gcc/testsuite/ChangeLog
gcc/testsuite/jit.dg/test-error-dereferencing-void-ptr.c [new file with mode: 0644]

index b17706e58ca3e12468b0ec566b5ce2fce2d95b74..01b3882053806381e6bb338e4d39920c784eca6e 100644 (file)
@@ -1,3 +1,12 @@
+2015-01-07  David Malcolm  <dmalcolm@redhat.com>
+
+       * jit-recording.h (gcc::jit::recording::type::is_void): New
+       virtual function.
+       (gcc::jit::recording::memento_of_get_type::is_void): New
+       function, overriding default implementation.
+       * libgccjit.c (gcc_jit_rvalue_dereference): Verify that
+       the underlying type is not "void".
+
 2015-01-07  David Malcolm  <dmalcolm@redhat.com>
 
        * docs/topics/expressions.rst (Unary Operations): Add
index 3734e9af5daf64257d2b855b855f50fa9014fd9a..9034e112be824aed79483077a80168a3c77eebc0 100644 (file)
@@ -443,6 +443,7 @@ public:
   virtual bool is_bool () const = 0;
   virtual type *is_pointer () = 0;
   virtual type *is_array () = 0;
+  virtual bool is_void () const { return false; }
 
   bool is_numeric () const
   {
@@ -494,6 +495,7 @@ public:
   bool is_bool () const;
   type *is_pointer () { return dereference (); }
   type *is_array () { return NULL; }
+  bool is_void () const { return m_kind == GCC_JIT_TYPE_VOID; }
 
 public:
   void replay_into (replayer *r);
index 6853bb0b20b25b7f11427e070484ff738b6e940a..99b2d56750e8248af1c4f759cb892f4fe3603129 100644 (file)
@@ -1664,6 +1664,13 @@ gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
     rvalue->get_debug_string (),
     rvalue->get_type ()->get_debug_string ());
 
+  RETURN_NULL_IF_FAIL_PRINTF2 (
+    !underlying_type->is_void (),
+    rvalue->m_ctxt, loc,
+    "dereference of void pointer %s (type: %s)",
+    rvalue->get_debug_string (),
+    rvalue->get_type ()->get_debug_string ());
+
   return (gcc_jit_lvalue *)rvalue->dereference (loc);
 }
 
index d6509c90952610c6ecbfcc412091848d78cdc730..356d20fe988bb0ec52d5d468dc4f3f6b48cefc84 100644 (file)
@@ -1,3 +1,7 @@
+2015-01-07  David Malcolm  <dmalcolm@redhat.com>
+
+       * jit.dg/test-error-dereferencing-void-ptr.c: New test case.
+
 2015-01-07  David Malcolm  <dmalcolm@redhat.com>
 
        * jit.dg/test-expressions.c (make_tests_of_unary_ops): Add test of
diff --git a/gcc/testsuite/jit.dg/test-error-dereferencing-void-ptr.c b/gcc/testsuite/jit.dg/test-error-dereferencing-void-ptr.c
new file mode 100644 (file)
index 0000000..a813f67
--- /dev/null
@@ -0,0 +1,96 @@
+#include <libgccjit.h>
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  /* Replay of API calls for ctxt.  */
+  gcc_jit_type *type_long_long =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG_LONG);
+  gcc_jit_type *type_void =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
+  gcc_jit_type *type_void_ptr =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
+  gcc_jit_field *field_u_signed =
+    gcc_jit_context_new_field (ctxt,
+                              NULL, /* gcc_jit_location *loc */
+                              type_long_long, /* gcc_jit_type *type, */
+                              "u_signed"); /* const char *name */
+  gcc_jit_field *field_u_ptr =
+    gcc_jit_context_new_field (ctxt,
+                              NULL, /* gcc_jit_location *loc */
+                              type_void_ptr, /* gcc_jit_type *type, */
+                              "u_ptr"); /* const char *name */
+  gcc_jit_field *fields_for_union_any[2] = {
+    field_u_signed,
+    field_u_ptr,
+  };
+  gcc_jit_type *union_any =
+    gcc_jit_context_new_union_type (ctxt,
+                                   NULL, /* gcc_jit_location *loc */
+                                   "any", /* const char *name */
+                                   2, /* int num_fields */
+                                   fields_for_union_any);
+  gcc_jit_function *func =
+    gcc_jit_context_new_function (ctxt, /* gcc_jit_context *ctxt */
+                                 NULL, /* gcc_jit_location *loc */
+                                 GCC_JIT_FUNCTION_EXPORTED,
+                                 type_void, /* gcc_jit_type *return_type */
+                                 "anonloop_0", /* const char *name */
+                                 0, /* int num_params */
+                                 NULL, /* gcc_jit_param **params */
+                                 0); /* int is_variadic */
+  gcc_jit_block *block_initial =
+    gcc_jit_function_new_block (func, "initial");
+
+  gcc_jit_lvalue *local_tmp =
+    gcc_jit_function_new_local (func, /* gcc_jit_function *func */
+                               NULL, /* gcc_jit_location *loc */
+                               union_any, /* gcc_jit_type *type */
+                               "tmp"); /* const char *name */
+
+  /* "tmp.u_signed = 0x213d640;" */
+  gcc_jit_block_add_assignment (
+    block_initial, /*gcc_jit_block *block */
+    NULL, /* gcc_jit_location *loc */
+    gcc_jit_lvalue_access_field (local_tmp, /*gcc_jit_lvalue *struct_or_union */
+                                NULL, /*gcc_jit_location *loc */
+                                field_u_signed),
+    gcc_jit_context_new_rvalue_from_long (
+      ctxt, /* gcc_jit_context *ctxt */
+      type_long_long, /* gcc_jit_type *numeric_type */
+      0x213d640)); /* long value */
+
+  /* "(*tmp.u_ptr) += 1;" which can't be done since u_ptr is a (void *).  */
+  gcc_jit_block_add_assignment_op (
+    block_initial, /*gcc_jit_block *block */
+    NULL, /* gcc_jit_location *loc */
+    /* "(*tmp.u_ptr)".  */
+    gcc_jit_rvalue_dereference (
+      gcc_jit_lvalue_as_rvalue (
+       gcc_jit_lvalue_access_field (
+         local_tmp, /*gcc_jit_lvalue *struct_or_union */
+         NULL, /*gcc_jit_location *loc */
+         field_u_ptr)),
+      NULL), /* gcc_jit_location *loc */
+    GCC_JIT_BINARY_OP_PLUS, /* enum gcc_jit_binary_op op */
+    gcc_jit_context_new_rvalue_from_int (
+      ctxt, /* gcc_jit_context *ctxt */
+      type_long_long, /* gcc_jit_type *numeric_type */
+      1)); /* int value */
+
+  gcc_jit_block_end_with_void_return (block_initial, /*gcc_jit_block *block */
+                                     NULL);
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  CHECK_VALUE (result, NULL);
+
+  /* Verify that the correct error message was emitted.  */
+  CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
+                     "gcc_jit_rvalue_dereference:"
+                     " dereference of void pointer tmp.u_ptr"
+                     " (type: void *)");
+}