File: get_set_context.c

package info (click to toggle)
valgrind 1%3A3.24.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 176,332 kB
  • sloc: ansic: 795,029; exp: 26,134; xml: 23,472; asm: 14,393; cpp: 9,397; makefile: 7,464; sh: 6,122; perl: 5,446; python: 1,498; javascript: 981; awk: 166; csh: 1
file content (78 lines) | stat: -rw-r--r-- 1,553 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Tests for various context functions
 *
 * getcontext
 * setcontext
 * swapcontext
 */

#include <ucontext.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <assert.h>
#include "../../memcheck.h"

int main(void)
{
   ucontext_t uc;
   volatile int flag = 0;
   if (-1 == getcontext(&uc)) {
      perror("getcontext failed: ");
   }

   flag++;

   if (flag == 1) {
      if (-1 == setcontext(&uc)) {
         perror("setcontext failed: ");
      }
   }

   flag++;

   if (flag == 3) {
      ucontext_t uc2;
      if (-1 == swapcontext(&uc2, &uc)) {
         perror("swapcontext failed: ");
      }
   }

   assert(flag == 5);
 
   // error section
   ucontext_t* ucp = malloc(sizeof(*ucp));
   ucontext_t* ucp2 = malloc(sizeof(*ucp2));
   (void)VALGRIND_MAKE_MEM_NOACCESS(ucp, sizeof(*ucp));
   (void)VALGRIND_MAKE_MEM_NOACCESS(ucp2, sizeof(*ucp2));
   flag = 0;
   if (-1 == getcontext(ucp)) {
      perror("getcontext failed: ");
   }

   flag++;

   if (flag == 1) {
      (void)VALGRIND_MAKE_MEM_NOACCESS(ucp, sizeof(*ucp));
      if (-1 == setcontext(ucp)) {
         perror("setcontext failed: ");
      }
      fprintf(stderr, "should never see setcontext return\n");
   }

   flag++;

   if (flag == 3) {
      (void)VALGRIND_MAKE_MEM_NOACCESS(ucp, sizeof(*ucp));
      if (-1 == swapcontext(ucp2, ucp)) {
         perror("swapcontext failed: ");
      }
      fprintf(stderr, "should never see swapcontest return\n");
   }

   assert(flag == 5);
   free(ucp);
   free(ucp2);
}