test-error-gcc_jit_rvalue_dereference_field-wrong-struct.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "libgccjit.h"
  4. #include "harness.h"
  5. struct foo
  6. {
  7. int x;
  8. int y;
  9. };
  10. struct bar
  11. {
  12. int p;
  13. int q;
  14. };
  15. void
  16. create_code (gcc_jit_context *ctxt, void *user_data)
  17. {
  18. /* Let's try to inject the equivalent of:
  19. void
  20. test_bogus_access (struct foo *f)
  21. {
  22. f->p = f->q;
  23. }
  24. i.e. using the wrong struct.
  25. */
  26. gcc_jit_type *void_type =
  27. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  28. gcc_jit_type *int_type =
  29. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  30. /* Map "struct foo". */
  31. gcc_jit_field *x =
  32. gcc_jit_context_new_field (ctxt,
  33. NULL,
  34. int_type,
  35. "x");
  36. gcc_jit_field *y =
  37. gcc_jit_context_new_field (ctxt,
  38. NULL,
  39. int_type,
  40. "y");
  41. gcc_jit_field *foo_fields[] = {x, y};
  42. gcc_jit_struct *struct_foo =
  43. gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 2, foo_fields);
  44. /* Map "struct bar". */
  45. gcc_jit_field *p =
  46. gcc_jit_context_new_field (ctxt,
  47. NULL,
  48. int_type,
  49. "p");
  50. gcc_jit_field *q =
  51. gcc_jit_context_new_field (ctxt,
  52. NULL,
  53. int_type,
  54. "q");
  55. /* We don't actually need a gcc_jit_type for "struct bar" for the test. */
  56. gcc_jit_field *bar_fields[] = {p, q};
  57. (void)gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 2, bar_fields);
  58. gcc_jit_type *foo_ptr =
  59. gcc_jit_type_get_pointer (gcc_jit_struct_as_type (struct_foo));
  60. /* Build the test function. */
  61. gcc_jit_param *param_f =
  62. gcc_jit_context_new_param (ctxt, NULL, foo_ptr, "f");
  63. gcc_jit_function *test_fn =
  64. gcc_jit_context_new_function (ctxt, NULL,
  65. GCC_JIT_FUNCTION_EXPORTED,
  66. void_type,
  67. "test_bogus_access",
  68. 1, &param_f,
  69. 0);
  70. /* Erroneous: f->p = ... */
  71. gcc_jit_lvalue *lvalue =
  72. gcc_jit_rvalue_dereference_field (
  73. gcc_jit_param_as_rvalue (param_f),
  74. NULL,
  75. p);
  76. /* Erroneous: ... = f->q; */
  77. gcc_jit_rvalue *rvalue =
  78. gcc_jit_lvalue_as_rvalue (
  79. gcc_jit_rvalue_dereference_field (
  80. gcc_jit_param_as_rvalue (param_f),
  81. NULL,
  82. q));
  83. gcc_jit_block *block =
  84. gcc_jit_function_new_block (test_fn, NULL);
  85. gcc_jit_block_add_assignment (
  86. block,
  87. NULL,
  88. lvalue, rvalue);
  89. gcc_jit_block_end_with_void_return (block, NULL);
  90. }
  91. void
  92. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  93. {
  94. CHECK_VALUE (result, NULL);
  95. /* Verify that the correct error message was emitted. */
  96. CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
  97. "gcc_jit_rvalue_dereference_field:"
  98. " p is not a field of struct foo");
  99. }