File: deadlock.c

package info (click to toggle)
libpulp 0.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,976 kB
  • sloc: ansic: 11,792; python: 1,216; sh: 881; makefile: 871; cpp: 582; asm: 387
file content (128 lines) | stat: -rw-r--r-- 3,424 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 *  libpulp - User-space Livepatching Library
 *
 *  Copyright (C) 2020-2021 SUSE Software Solutions GmbH
 *
 *  This file is part of libpulp.
 *
 *  libpulp is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  libpulp is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with libpulp.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <dlfcn.h>
#include <errno.h>
#include <malloc.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* At least one thread is required to cause the deadlock, otherwise,
 * malloc/calloc refrain from acquiring a lock, at all.
 */
#define THREADS 15

/* A live-patchable library is required so that a live patch can be
 * applied, however, this test case only relies on the fact that
 * multiple threads in the application use malloc/calloc, not on
 * anything specific to libblocked (any live-patchable library could
 * have been used).
 */
#include <libblocked.h>

/* Do not optimize, otherwise the calloc/free sequences go away. */
#ifndef __clang__
#pragma GCC push_options
#pragma GCC optimize("O0")
#else
#pragma clang optimize off
#endif

/* Calls many of the functions that acquire the locks that might cause
 * live-patch installation to deadlock.
 */
void *
worker(void *arg __attribute__((unused)))
{
  void *handle;
  void *symbol __attribute__((unused));
  char *data;

  while (1) {
    data = calloc(100, sizeof(char));
    data = reallocarray(data, 200, sizeof(char));
    free(data);
    data = malloc(300 * sizeof(char));
    data = realloc(data, 400 * sizeof(char));
    free(data);
    data = aligned_alloc(256, 500 * sizeof(char));
    free(data);
    data = valloc(600 * sizeof(char));
    free(data);
    data = pvalloc(700 * sizeof(char));
    free(data);
    if (!posix_memalign((void **)&data, 512, 800 * sizeof(char)))
      free(data);

    handle = dlopen(NULL, RTLD_LAZY | RTLD_NOLOAD);
    if (handle != NULL) {
      symbol = dlsym(handle, "worker");
      dlclose(handle);
    }

    /* Sleep for a brief while to increase the chances that the
     * application of the live-patch succeeds.
     */
    usleep(1);
  }
}

/* Restore optimization level. */
#ifndef __clang__
#pragma GCC pop_options
#else
#pragma clang optimize on
#endif

int
main(void)
{
  char buffer[128];
  pthread_t thread[THREADS];

  /* Create THREADS threads that try to allocate and free memory in an
   * infinite loop.
   */
  for (int iter = 0; iter < THREADS; iter++)
    pthread_create(&thread[iter], NULL, worker, NULL);

  /* Signal readiness. */
  printf("Waiting for input.\n");

  /* Wait for any input, then call hello(), which should produce
   * different output before and after the live-patching.
   */
  while (1) {
    if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
      if (errno) {
        perror("deadlock");
        return 1;
      }
      printf("Reached the end of file; quitting.\n");
      return 0;
    }
    hello();
  }

  return 0;
}