File: memory.bs

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (340 lines) | stat: -rw-r--r-- 8,027 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use progvis:program;

/**
 * Memory access.
 *
 * Used to report whether data was read from and/or written to. Also reports which threads read and
 * wrote to the data for the model checker.
 *
 * The class makes a distinction between "new" reads/writes and "old" reads/writes. This is mainly
 * for the benefit of the visualization.
 *
 * 'error' is used to indicate if an error was present.
 */
value MemAccess {
	// Reads - 0=none, 1=old, 2>=new
	Byte read;
	// Writes - 0=none, 1=old, 2>=new
	Byte write;
	// Read error?
	Bool readError;
	// Write error?
	Bool writeError;

	// Exact reads+writes. This is mainly for the model checker. Contains the operations that are
	// important for the current memory checker, this may not overlap entirely with the values in
	// 'read' and 'write'. Stores thread ID:s of writing threads, followed by a zero, followed by
	// thread ID:s of reading threads.
	Nat[] threads;

	// Create.
	init() {
		init {}
	}

	// Clear everything.
	void clear() {
		read = 0;
		write = 0;
		readError = false;
		writeError = false;
		threads.clear();
	}

	// Any error?
	Bool error() {
		readError | writeError;
	}
}


/**
 * Memory model to use.
 */
enum MemoryChecks {
	// Don't use memory accesses at all in the visualization, not even for highlighting.
	none,

	// No checks at all, but visualizes accesses. Essentially sequential consistency.
	sequential,

	// Only check individual statements.
	statements,

	// Check between barriers. This is what the C/C++ standard requires (except for relaxed atomics)
	barriers,
}


// Create a memory check.
MemCheck create(MemoryChecks checks) {
	if (checks == MemoryChecks:none)
		ZeroMemCheck();
	else if (checks == MemoryChecks:sequential)
		NoMemCheck();
	else if (checks == MemoryChecks:statements)
		StmtMemCheck();
	else
		FullMemCheck();
}

/**
 * Object that keeps track of all reads and writes by threads during a traversal.
 *
 * It is possible to ask this object which threads read and write certain data, and to detect race
 * conditions from those observations. Collects information on any instances where reads and writes
 * cause race conditions.
 *
 * Abstract base class, there are implementations for the different models.
 */
class MemCheck {
	// Memory trackers for each of the threads currently alive. Mapped thread-id->tracker.
	protected Nat->MemTracker threads;

	// Error messages we produced.
	protected Str[] errors;

	// Add a thread.
	void add(Nat id, MemTracker tracker) {
		threads.put(id, tracker);
	}

	// Get errors, if any.
	ProgramError? error() {
		if (errors.any)
			return DataRaceError(errors);
		else
			return null;
	}

	// Query the memory access for some data. 'old' is used to inhibit old errors, for example.
	MemAccess query(MemAccess old, unsafe:RawPtr object, Nat offset, Nat size) : abstract;
}


/**
 * Mem check implementation that does not highlight anything. Mostly for programmatical use of
 * Progvis, where the highlights would be confusing.
 */
class ZeroMemCheck extends MemCheck {
	// Query the memory access for some data.
	MemAccess query(MemAccess old, unsafe:RawPtr object, Nat offset, Nat size) : override {
		return MemAccess();
	}
}


/**
 * Mem check implementation without checks, for sequential. Highlights reads and writes for clarity.
 */
class NoMemCheck extends MemCheck {
	// Query the memory access for some data.
	MemAccess query(MemAccess old, unsafe:RawPtr object, Nat offset, Nat size) : override {
		MemAccess result;

		// Check reads and writes for the visualization. We don't check correctness.
		for (k, v in threads) {
			if (v.newWrites.has(object, offset, size)) {
				result.write = 2;
			}
			if (v.newReads.has(object, offset, size)) {
				result.read = 2;
			}
		}

		// Note: We don't touch "threads" as we don't check for errors.

		result;
	}
}


/**
 * Mem check implementation that only considers new reads/writes.
 */
class StmtMemCheck extends MemCheck {
	// Query the memory access for some data.
	MemAccess query(MemAccess old, unsafe:RawPtr object, Nat offset, Nat size) : override {
		MemAccess result;

		// Check writes.
		for (k, v in threads) {
			if (v.newWrites.has(object, offset, size)) {
				result.write = 2;
				result.threads << k;
			}
		}

		if (result.threads.count > 1) {
			if (!old.writeError)
				dualWriteError(result.threads);
			result.writeError = true;
		}

		result.threads << 0;
		Nat writtenBy = result.threads[0];

		// Check reads.
		for (k, v in threads) {
			if (v.newReads.has(object, offset, size)) {
				if ((writtenBy != 0) & (writtenBy != k))
					result.readError = true;
				result.read = 2;
				result.threads << k;
			}
		}

		if (result.readError) {
			if (!old.readError) {
				readWriteError(result.threads);
			}
		}

		result;
	}

	// Add an error message concerning multiple writes.
	private void dualWriteError(Nat[] writes) {
		writes.sort();

		StrBuf result;
		result << "Threads " << join(writes, ", ") << " all wrote to the same value concurrently.";
		errors << result.toS;
	}

	// Add an error message concerning read-write errors.
	private void readWriteError(Nat[] threads) {
		Nat[] writes;
		Nat[] reads;

		Bool zero = false;
		for (v in threads) {
			if (v == 0) {
				zero = true;
			} else if (zero) {
				reads << v;
			} else {
				writes << v;
			}
		}

		writes.sort();
		reads.sort();

		StrBuf result;
		if (writes.count > 1)
			result << "Threads ";
		else
			result << "Thread ";
		result << join(writes, ", ") << " wrote to a value that ";
		if (reads.count > 1)
			result << "threads ";
		else
			result << "thread ";
		result << join(reads, ", ") << " read from concurrently.";
		errors << result.toS;
	}
}


/**
 * Object that keeps track of all reads and writes by threads during a traversal. Takes barrier
 * information into account.
 */
class FullMemCheck extends MemCheck {
	// Query the memory access for some data.
	// This is the fast-path: it does not allocate memory in the case no errors happen
	// (which is the normal case). It calls one of the error functions in case of an error.
	// These will have to re-do parts of the work in order to provide better error messages,
	// but as they are on the slow path, this is fine.
	MemAccess query(MemAccess old, unsafe:RawPtr object, Nat offset, Nat size) : override {
		MemAccess result;

		// Check writes.
		for (k, v in threads) {
			if (v.writes.has(object, offset, size)) {
				result.write |= 1;
				result.threads << k;

				if (v.newWrites.has(object, offset, size)) {
					result.write = 2;
				}
			}
		}

		if (result.threads.count > 1) {
			if (!old.writeError)
				dualWriteError(result.threads);
			result.writeError = true;
		}

		result.threads << 0;
		Nat writtenBy = result.threads[0];

		// Check reads.
		for (k, v in threads) {
			if (v.reads.has(object, offset, size)) {
				if ((writtenBy != 0) & (writtenBy != k))
					result.readError = true;
				result.read |= 1;
				result.threads << k;

				if (v.newReads.has(object, offset, size)) {
					result.read = 2;
				}
			}
		}

		if (result.readError) {
			if (!old.readError) {
				readWriteError(result.threads);
			}
		}

		result;
	}


	// Add an error message concerning multiple writes.
	private void dualWriteError(Nat[] writes) {
		writes.sort();

		StrBuf result;
		result << "Threads " << join(writes, ", ") << " all wrote to the same value concurrently.";
		errors << result.toS;
	}

	// Add an error message concerning read-write errors.
	private void readWriteError(Nat[] threads) {
		Nat[] writes;
		Nat[] reads;

		Bool zero = false;
		for (v in threads) {
			if (v == 0) {
				zero = true;
			} else if (zero) {
				reads << v;
			} else {
				writes << v;
			}
		}

		writes.sort();
		reads.sort();

		StrBuf result;
		if (writes.count > 1)
			result << "Threads ";
		else
			result << "Thread ";
		result << join(writes, ", ") << " wrote to a value that ";
		if (reads.count > 1)
			result << "threads ";
		else
			result << "thread ";
		result << join(reads, ", ") << " read from concurrently.";
		errors << result.toS;
	}

}