summaryrefslogtreecommitdiffstats
path: root/src/cmd/cgo/internal/testsanitizers/testdata/tsan13.go
blob: ebdf63581b0a23f9591b1c7702492dc1998d0a65 (plain)
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
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

// This program failed when run under the C/C++ ThreadSanitizer.
// There was no TSAN synchronization for the call to the cgo
// traceback routine.

/*
#cgo CFLAGS: -g -fsanitize=thread
#cgo LDFLAGS: -g -fsanitize=thread

#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

struct tracebackArg {
	uintptr_t  Context;
	uintptr_t  SigContext;
	uintptr_t* Buf;
	uintptr_t  Max;
};

void tsanTraceback(struct tracebackArg *arg) {
	arg->Buf[0] = 0;
}

static void* spin(void *arg) {
	size_t n;
	struct timeval tvstart, tvnow;
	int diff;
	void *prev;
	void *cur;

	prev = NULL;
	gettimeofday(&tvstart, NULL);
	for (n = 0; n < 1<<20; n++) {
		cur = malloc(n);
		free(prev);
		prev = cur;

		gettimeofday(&tvnow, NULL);
		diff = (tvnow.tv_sec - tvstart.tv_sec) * 1000 * 1000 + (tvnow.tv_usec - tvstart.tv_usec);

		// Profile frequency is 100Hz so we should definitely
		// get some signals in 50 milliseconds.
		if (diff > 50 * 1000) {
			break;
		}
	}

	free(prev);

	return NULL;
}

static void runThreads(int n) {
	pthread_t ids[64];
	int i;

	if (n > 64) {
		n = 64;
	}
	for (i = 0; i < n; i++) {
		pthread_create(&ids[i], NULL, spin, NULL);
	}
	for (i = 0; i < n; i++) {
		pthread_join(ids[i], NULL);
	}
}
*/
import "C"

import (
	"io"
	"runtime"
	"runtime/pprof"
	"unsafe"
)

func main() {
	runtime.SetCgoTraceback(0, unsafe.Pointer(C.tsanTraceback), nil, nil)
	pprof.StartCPUProfile(io.Discard)
	C.runThreads(C.int(runtime.GOMAXPROCS(0)))
	pprof.StopCPUProfile()
}