Skip to content

Commit 76d8f03

Browse files
committed
Make the task allocator verify stack discipline in the laziest possible way.
1 parent c31dab2 commit 76d8f03

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

stdlib/public/Concurrency/TaskAlloc.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,33 @@
1919

2020
#include "TaskPrivate.h"
2121
#include "swift/Runtime/Concurrency.h"
22+
#include "swift/Runtime/Debug.h"
2223
#include <stdlib.h>
24+
#include <vector>
2325

2426
using namespace swift;
2527

2628
namespace {
2729

2830
class TaskAllocator {
31+
// Just keep track of all allocations in a vector so that we can
32+
// verify stack discipline. We should make sure the allocator
33+
// implementation strictly verifies allocation order at least
34+
// until we've stabilized the compiler implementation.
35+
std::vector<void*> Allocations;
36+
2937
public:
3038
void *alloc(size_t size) {
31-
return malloc(size);
39+
void *ptr = malloc(size);
40+
Allocations.push_back(ptr);
41+
return ptr;
3242
}
3343

3444
void dealloc(void *ptr) {
45+
if (Allocations.empty() || Allocations.back() != ptr)
46+
fatalError(0, "pointer was not the last allocation on this task");
47+
48+
Allocations.pop_back();
3549
free(ptr);
3650
}
3751
};
@@ -50,7 +64,14 @@ void swift::_swift_task_alloc_initialize(AsyncTask *task) {
5064
}
5165

5266
static TaskAllocator &allocator(AsyncTask *task) {
53-
return reinterpret_cast<TaskAllocator &>(task->AllocatorPrivate);
67+
if (task)
68+
return reinterpret_cast<TaskAllocator &>(task->AllocatorPrivate);
69+
70+
// FIXME: this fall-back shouldn't be necessary, but it's useful
71+
// for now, since the current execution tests aren't setting up a task
72+
// properly.
73+
static TaskAllocator global;
74+
return global;
5475
}
5576

5677
void swift::_swift_task_alloc_destroy(AsyncTask *task) {

0 commit comments

Comments
 (0)