1
0

privates.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <lib9.h>
  11. extern void **_privates;
  12. extern int _nprivates;
  13. void
  14. main(void)
  15. {
  16. unsigned char buf[512];
  17. uint64_t i, fail = 0;
  18. /* _nprivates is a private things of libc, over which we should
  19. * never do assumptions. So we are breaking the interface here:
  20. * let's just do it once, to read how many privates we have at
  21. * the beginning. But note that nowhere is written that
  22. * _nprivate is kept constant!
  23. */
  24. int nprivatesAtStart = _nprivates;
  25. if (_privates == nil) {
  26. fprint(2, "_privates is nil\n");
  27. fail++;
  28. }
  29. if (nprivatesAtStart == 0) {
  30. fprint(2, "_nprivates is 0\n");
  31. fail++;
  32. }
  33. for (i = 0; i < nprivatesAtStart; i++) {
  34. _privates[i] = (void *)(0x77665544332210 + i);
  35. }
  36. memset(buf, 0, sizeof buf);
  37. for (i = 0; i < nprivatesAtStart; i++) {
  38. if (_privates[i] != (void *)(0x77665544332210 + i)){
  39. fprint(2, "_privates[%d] = %p\n", i, _privates[i]);
  40. fail++;
  41. }
  42. }
  43. void*** p = malloc(nprivatesAtStart*sizeof(void**));
  44. for (i = 0; i < nprivatesAtStart; i++) {
  45. p[i] = privalloc();
  46. if(p[i] == nil){
  47. fail++;
  48. fprint(2, "privalloc[%d] nil: %p\n", i, p[i]);
  49. }
  50. }
  51. p[i] = privalloc();
  52. if(p[i] != nil){
  53. fail++;
  54. fprint(2, "privalloc[_nprivates (%d)] not nil: %p\n", i, p[i]);
  55. }
  56. for (i = 0; i < nprivatesAtStart; i++) {
  57. *(p[i]) = (void*)i;
  58. }
  59. for (i = 0; i < nprivatesAtStart; i++) {
  60. if(*(p[i]) != (void*)i){
  61. fprint(2, "p[%d] != %d\n", i, i);
  62. fail++;
  63. }
  64. }
  65. if (fail > 0) {
  66. print("FAIL (%d times)\n", fail);
  67. exits("FAIL");
  68. }
  69. print("PASS\n");
  70. exits("PASS");
  71. }