File: stack_switch.c

package info (click to toggle)
valgrind 1%3A3.10.0-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 97,452 kB
  • ctags: 62,380
  • sloc: ansic: 589,429; xml: 21,096; exp: 8,751; cpp: 7,366; asm: 6,526; perl: 5,656; sh: 5,334; makefile: 4,946; haskell: 195
file content (57 lines) | stat: -rw-r--r-- 1,179 bytes parent folder | download | duplicates (3)
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
#define _XOPEN_SOURCE 600
#define _BSD_SOURCE
#define _GNU_SOURCE

#include <stdio.h>

#include <sched.h>
#include <stdlib.h>
#include <string.h>
#include "tests/sys_mman.h"
#include <sys/syscall.h>
#include <sys/wait.h>
#include <unistd.h>

#include "valgrind.h"

#define STACK_SIZE 8192

#ifndef CLONE_THREAD
#define CLONE_THREAD	0x00010000	/* Same thread group? */
#endif

static int thread_main(void *arg)
{
   char buffer[1024];

   memset( buffer, 1, sizeof( buffer ) );

   sleep(2); /* ppc64-linux hack */
   return memchr( buffer, 1, sizeof( buffer ) ) == NULL;
}

int main(int argc, char **argv)
{
   void *stack;
   int stackid __attribute__((unused));
   pid_t pid;

   /* "2*" is a ppc64-linux hack */
   if ( ( stack = mmap( NULL, 2* STACK_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 ) ) == MAP_FAILED )
   {
      perror( "mmap" );
      exit( 1 );
   }

   stackid = VALGRIND_STACK_REGISTER( stack, stack + STACK_SIZE );

   if ( ( pid = clone( thread_main, stack + STACK_SIZE, CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|SIGCHLD, NULL ) ) == -1 )
   {
      perror( "clone" );
      exit( 1 );
   }

   sleep( 1 );

   exit( 0 );
}