File: mach_vm_allocate.c

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (89 lines) | stat: -rw-r--r-- 2,253 bytes parent folder | download | duplicates (23)
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
// Test that mach_vm_[de]allocate resets shadow memory status.
//
// RUN: %clang_tsan %s -o %t
// RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'

// <mach/mach_vm.h> is not provided by the simulator SDK.
// UNSUPPORTED: iossim

#include <mach/mach.h>
#include <mach/mach_vm.h>
#include <pthread.h>
#include <assert.h>
#include <stdio.h>

#include "../test.h"

const mach_vm_size_t alloc_size = sizeof(int);
static int *global_ptr;

static int *alloc() {
  mach_vm_address_t addr;
  kern_return_t kr =
      mach_vm_allocate(mach_task_self(), &addr, alloc_size, VM_FLAGS_ANYWHERE);
  assert(kr == KERN_SUCCESS);
  return (int *)addr;
}

static void alloc_fixed(int *ptr) {
  mach_vm_address_t addr = (mach_vm_address_t)ptr;
  // Re-allocation via VM_FLAGS_FIXED sporadically fails.
  kern_return_t kr =
      mach_vm_allocate(mach_task_self(), &addr, alloc_size, VM_FLAGS_FIXED);
  if (kr != KERN_SUCCESS)
    global_ptr = NULL;
}

static void dealloc(int *ptr) {
  kern_return_t kr =
      mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)ptr, alloc_size);
  assert(kr == KERN_SUCCESS);
}

static void *Thread(void *arg) {
  *global_ptr = 7;  // Assignment 1

  // We want to test that TSan does not report a race between the two
  // assignments to *global_ptr when the underlying memory is re-allocated
  // between assignments. The calls to the API itself are racy though, so ignore
  // them.
  AnnotateIgnoreWritesBegin(__FILE__, __LINE__);
  dealloc(global_ptr);
  alloc_fixed(global_ptr);
  AnnotateIgnoreWritesEnd(__FILE__, __LINE__);

  barrier_wait(&barrier);
  return NULL;
}

static bool try_realloc_on_same_address() {
  barrier_init(&barrier, 2);
  global_ptr = alloc();
  pthread_t t;
  pthread_create(&t, NULL, Thread, NULL);

  barrier_wait(&barrier);
  if (global_ptr)
    *global_ptr = 8;  // Assignment 2

  pthread_join(t, NULL);
  dealloc(global_ptr);

  return global_ptr != NULL;
}

int main(int argc, const char *argv[]) {
  bool success;
  for (int i = 0; i < 10; i++) {
    success = try_realloc_on_same_address();
    if (success) break;
  }

  if (!success)
    fprintf(stderr, "Unable to set up testing condition; silently pass test\n");

  printf("Done.\n");
  return 0;
}

// CHECK: Done.