cls_pointer_stack.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Area: ffi_call, closure_call
  2. Purpose: Check pointer arguments across multiple hideous stack frames.
  3. Limitations: none.
  4. PR: none.
  5. Originator: Blake Chaffin 6/7/2007 */
  6. /* { dg-do run { xfail strongarm*-*-* xscale*-*-* } } */
  7. #include "ffitest.h"
  8. static long dummyVar;
  9. long dummy_func(
  10. long double a1, char b1,
  11. long double a2, char b2,
  12. long double a3, char b3,
  13. long double a4, char b4)
  14. {
  15. return a1 + b1 + a2 + b2 + a3 + b3 + a4 + b4;
  16. }
  17. void* cls_pointer_fn2(void* a1, void* a2)
  18. {
  19. long double trample1 = (intptr_t)a1 + (intptr_t)a2;
  20. char trample2 = ((char*)&a1)[0] + ((char*)&a2)[0];
  21. long double trample3 = (intptr_t)trample1 + (intptr_t)a1;
  22. char trample4 = trample2 + ((char*)&a1)[1];
  23. long double trample5 = (intptr_t)trample3 + (intptr_t)a2;
  24. char trample6 = trample4 + ((char*)&a2)[1];
  25. long double trample7 = (intptr_t)trample5 + (intptr_t)trample1;
  26. char trample8 = trample6 + trample2;
  27. void* result;
  28. dummyVar = dummy_func(trample1, trample2, trample3, trample4,
  29. trample5, trample6, trample7, trample8);
  30. result = (void*)((intptr_t)a1 + (intptr_t)a2);
  31. printf("0x%08x 0x%08x: 0x%08x\n",
  32. (unsigned int)(uintptr_t) a1,
  33. (unsigned int)(uintptr_t) a2,
  34. (unsigned int)(uintptr_t) result);
  35. return result;
  36. }
  37. void* cls_pointer_fn1(void* a1, void* a2)
  38. {
  39. long double trample1 = (intptr_t)a1 + (intptr_t)a2;
  40. char trample2 = ((char*)&a1)[0] + ((char*)&a2)[0];
  41. long double trample3 = (intptr_t)trample1 + (intptr_t)a1;
  42. char trample4 = trample2 + ((char*)&a1)[1];
  43. long double trample5 = (intptr_t)trample3 + (intptr_t)a2;
  44. char trample6 = trample4 + ((char*)&a2)[1];
  45. long double trample7 = (intptr_t)trample5 + (intptr_t)trample1;
  46. char trample8 = trample6 + trample2;
  47. void* result;
  48. dummyVar = dummy_func(trample1, trample2, trample3, trample4,
  49. trample5, trample6, trample7, trample8);
  50. result = (void*)((intptr_t)a1 + (intptr_t)a2);
  51. printf("0x%08x 0x%08x: 0x%08x\n",
  52. (unsigned int)(intptr_t) a1,
  53. (unsigned int)(intptr_t) a2,
  54. (unsigned int)(intptr_t) result);
  55. result = cls_pointer_fn2(result, a1);
  56. return result;
  57. }
  58. static void
  59. cls_pointer_gn(ffi_cif* cif __UNUSED__, void* resp,
  60. void** args, void* userdata __UNUSED__)
  61. {
  62. void* a1 = *(void**)(args[0]);
  63. void* a2 = *(void**)(args[1]);
  64. long double trample1 = (intptr_t)a1 + (intptr_t)a2;
  65. char trample2 = ((char*)&a1)[0] + ((char*)&a2)[0];
  66. long double trample3 = (intptr_t)trample1 + (intptr_t)a1;
  67. char trample4 = trample2 + ((char*)&a1)[1];
  68. long double trample5 = (intptr_t)trample3 + (intptr_t)a2;
  69. char trample6 = trample4 + ((char*)&a2)[1];
  70. long double trample7 = (intptr_t)trample5 + (intptr_t)trample1;
  71. char trample8 = trample6 + trample2;
  72. dummyVar = dummy_func(trample1, trample2, trample3, trample4,
  73. trample5, trample6, trample7, trample8);
  74. *(void**)resp = cls_pointer_fn1(a1, a2);
  75. }
  76. int main (void)
  77. {
  78. ffi_cif cif;
  79. void *code;
  80. ffi_closure* pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  81. void* args[3];
  82. /* ffi_type cls_pointer_type; */
  83. ffi_type* arg_types[3];
  84. /* cls_pointer_type.size = sizeof(void*);
  85. cls_pointer_type.alignment = 0;
  86. cls_pointer_type.type = FFI_TYPE_POINTER;
  87. cls_pointer_type.elements = NULL;*/
  88. void* arg1 = (void*)0x01234567;
  89. void* arg2 = (void*)0x89abcdef;
  90. ffi_arg res = 0;
  91. arg_types[0] = &ffi_type_pointer;
  92. arg_types[1] = &ffi_type_pointer;
  93. arg_types[2] = NULL;
  94. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_pointer,
  95. arg_types) == FFI_OK);
  96. args[0] = &arg1;
  97. args[1] = &arg2;
  98. args[2] = NULL;
  99. printf("\n");
  100. ffi_call(&cif, FFI_FN(cls_pointer_fn1), &res, args);
  101. printf("res: 0x%08x\n", (unsigned int) res);
  102. /* { dg-output "\n0x01234567 0x89abcdef: 0x8acf1356" } */
  103. /* { dg-output "\n0x8acf1356 0x01234567: 0x8bf258bd" } */
  104. /* { dg-output "\nres: 0x8bf258bd" } */
  105. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_pointer_gn, NULL, code) == FFI_OK);
  106. res = (ffi_arg)(uintptr_t)((void*(*)(void*, void*))(code))(arg1, arg2);
  107. printf("res: 0x%08x\n", (unsigned int) res);
  108. /* { dg-output "\n0x01234567 0x89abcdef: 0x8acf1356" } */
  109. /* { dg-output "\n0x8acf1356 0x01234567: 0x8bf258bd" } */
  110. /* { dg-output "\nres: 0x8bf258bd" } */
  111. exit(0);
  112. }