test-error-dereference-read-of-non-pointer.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "libgccjit.h"
  4. #include "harness.h"
  5. void
  6. create_code (gcc_jit_context *ctxt, void *user_data)
  7. {
  8. /* Let's try to inject the equivalent of:
  9. void
  10. int test_bogus_dereference_read (int i)
  11. {
  12. return *i;
  13. }
  14. i.e. where i is *not* a pointer.
  15. */
  16. gcc_jit_type *void_type =
  17. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  18. gcc_jit_type *int_type =
  19. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  20. /* Build the test function. */
  21. gcc_jit_param *param_i =
  22. gcc_jit_context_new_param (ctxt, NULL, int_type, "i");
  23. gcc_jit_function *test_fn =
  24. gcc_jit_context_new_function (ctxt, NULL,
  25. GCC_JIT_FUNCTION_EXPORTED,
  26. void_type,
  27. "test_bogus_dereference_read",
  28. 1, &param_i,
  29. 0);
  30. gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
  31. /* Erroneous: "return *i;" */
  32. gcc_jit_block_end_with_return (
  33. block,
  34. NULL,
  35. gcc_jit_lvalue_as_rvalue (
  36. gcc_jit_rvalue_dereference (
  37. gcc_jit_param_as_rvalue (param_i),
  38. NULL)));
  39. }
  40. void
  41. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  42. {
  43. CHECK_VALUE (result, NULL);
  44. /* Verify that the correct error message was emitted. */
  45. CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
  46. ("gcc_jit_rvalue_dereference:"
  47. " dereference of non-pointer i (type: int)"));
  48. }