1
0

longjmp.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <u.h>
  2. #include <lib9.h>
  3. // from kernel's mem.h
  4. #define MiB (1024*1024)
  5. #define TSTKTOP (0x00007ffffffff000ull)
  6. #define USTKSIZE (16*MiB) /* size of user stack */
  7. #define USTKTOP (TSTKTOP-USTKSIZE) /* end of new stack in sysexec */
  8. enum {
  9. Njmps = 10000
  10. };
  11. void foo(void);
  12. void
  13. main(void)
  14. {
  15. int i, njmp;
  16. int fail = 0;
  17. jmp_buf label;
  18. njmp = 0;
  19. while((njmp = setjmp(label)) < Njmps)
  20. longjmp(label, njmp+1);
  21. for(i = 0; i < nelem(label); i++)
  22. fprint(2, "label[%d] = %p\n", i, label[i]);
  23. fprint(2, "main: %p foo: %p\n", main, foo);
  24. if(njmp != Njmps){
  25. print("error: njmp = %d\n", njmp);
  26. fail++;
  27. }
  28. if(label[JMPBUFPC] < (uintptr_t)main){
  29. print("error: label[JMPBUFPC] = %#p\n", label[JMPBUFPC]);
  30. fail++;
  31. }
  32. if(label[JMPBUFPC] > (uintptr_t)foo){
  33. print("error: label[JMPBUFPC] = %#p\n", label[JMPBUFPC]);
  34. fail++;
  35. }
  36. if(label[JMPBUFSP] > (uintptr_t)&label[nelem(label)]){
  37. print("error: label[JMPBUFSP] (%#p) is greater then &label[nelem(label)] (%#p) \n", label[JMPBUFPC], &label[nelem(label)]);
  38. fail++;
  39. }
  40. if(label[JMPBUFSP] < USTKTOP-USTKSIZE){
  41. print("error: label[JMPBUFSP] (%#p) is lower then USTKTOP-USTKSIZE \n", label[JMPBUFSP]);
  42. fail++;
  43. }
  44. if(fail == 0){
  45. print("PASS\n");
  46. exits("PASS");
  47. }
  48. print("FAIL\n");
  49. exits("FAIL");
  50. }
  51. void
  52. foo(void)
  53. {
  54. }