longjmp.c 697 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <u.h>
  2. #include <lib9.h>
  3. enum {
  4. Njmps = 10000
  5. };
  6. void foo(void);
  7. void
  8. main(void)
  9. {
  10. int i, njmp;
  11. int fail = 0;
  12. jmp_buf label;
  13. njmp = 0;
  14. while((njmp = setjmp(label)) < Njmps)
  15. longjmp(label, njmp+1);
  16. for(i = 0; i < nelem(label); i++)
  17. fprint(2, "label[%d] = %p\n", i, label[i]);
  18. fprint(2, "main: %p foo: %p\n", main, foo);
  19. if(njmp != Njmps)
  20. fail++;
  21. if(label[JMPBUFPC] < (uintptr_t)main)
  22. fail++;
  23. if(label[JMPBUFPC] > (uintptr_t)foo)
  24. fail++;
  25. if(label[JMPBUFSP] > (uintptr_t)&label[nelem(label)])
  26. fail++;
  27. if(label[JMPBUFSP] < 0x7fffffd00000)
  28. fail++;
  29. if(fail == 0){
  30. print("PASS\n");
  31. exits("PASS");
  32. }
  33. print("FAIL\n");
  34. exits("FAIL");
  35. }
  36. void
  37. foo(void)
  38. {
  39. }