Skip to content

Commit 3a7c0db

Browse files
nyhwkozaczuk
authored andcommitted
sched: free aligned_alloc() with free()
Memory allocated with aligned_alloc() should be freed with free(), not with delete - although in our actual implemention those are really the same thing. gcc 11 warns about using sched::thread::make() - which uses align_alloc() - and the deleting it with "delete", using std::unique_ptr. Gcc only warns about this issue in sched.cc so we only fix that file, but note that other places of the code also have unique_ptr<thread> and should eventually use a similar fix. The fix is a new method sched::thread::make_unique<> which returns a std::unique_ptr that holds a thread a knows how to properly delete it. Signed-off-by: Nadav Har'El <[email protected]> Message-Id: <[email protected]>
1 parent b37a3fc commit 3a7c0db

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

core/sched.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class thread::reaper {
135135
private:
136136
mutex _mtx;
137137
std::list<thread*> _zombies;
138-
std::unique_ptr<thread> _thread;
138+
thread_unique_ptr _thread;
139139
};
140140

141141
cpu::cpu(unsigned _id)
@@ -552,7 +552,7 @@ void thread::pin(cpu *target_cpu)
552552
// load balancer thread) but a "good-enough" dirty solution is to
553553
// temporarily create a new ad-hoc thread, "wakeme".
554554
bool do_wakeme = false;
555-
std::unique_ptr<thread> wakeme(thread::make([&] () {
555+
thread_unique_ptr wakeme(thread::make_unique([&] () {
556556
wait_until([&] { return do_wakeme; });
557557
t.wake();
558558
}, sched::thread::attr().pin(source_cpu)));
@@ -590,7 +590,7 @@ void thread::pin(thread *t, cpu *target_cpu)
590590
// where the target thread is currently running. We start here a new
591591
// helper thread to follow the target thread's CPU. We could have also
592592
// re-used an existing thread (e.g., the load balancer thread).
593-
std::unique_ptr<thread> helper(thread::make([&] {
593+
thread_unique_ptr helper(thread::make_unique([&] {
594594
WITH_LOCK(irq_lock) {
595595
// This thread started on the same CPU as t, but by now t might
596596
// have moved. If that happened, we need to move too.
@@ -701,7 +701,7 @@ void thread::unpin()
701701
}
702702
return;
703703
}
704-
std::unique_ptr<thread> helper(thread::make([this] {
704+
thread_unique_ptr helper(thread::make_unique([this] {
705705
WITH_LOCK(preempt_lock) {
706706
// helper thread started on the same CPU as "this", but by now
707707
// "this" might migrated. If that happened helper need to migrate.
@@ -973,7 +973,7 @@ thread::thread(std::function<void ()> func, attr attr, bool main, bool app)
973973
, _migration_lock_counter(0)
974974
, _pinned(false)
975975
, _id(0)
976-
, _cleanup([this] { delete this; })
976+
, _cleanup([this] { dispose(this); })
977977
, _app(app)
978978
, _joiner(nullptr)
979979
{
@@ -1600,7 +1600,7 @@ bool operator<(const timer_base& t1, const timer_base& t2)
16001600
}
16011601

16021602
thread::reaper::reaper()
1603-
: _mtx{}, _zombies{}, _thread(thread::make([=] { reap(); }))
1603+
: _mtx{}, _zombies{}, _thread(thread::make_unique([=] { reap(); }))
16041604
{
16051605
_thread->start();
16061606
}

include/osv/sched.hh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,19 @@ public:
428428
}
429429
return new(p) thread(std::forward<Args>(args)...);
430430
}
431+
// Since make() doesn't necessarily allocate with "new", dispose() should
432+
// be used to free it, not "delete". However, in practice our new and
433+
// delete are the same, so delete is fine.
434+
static void dispose(thread* p) {
435+
p->~thread();
436+
free(p);
437+
}
438+
using thread_unique_ptr = std::unique_ptr<thread, decltype(&thread::dispose)>;
439+
template <typename... Args>
440+
static thread_unique_ptr make_unique(Args&&... args) {
441+
return thread_unique_ptr(make(std::forward<Args>(args)...),
442+
thread::dispose);
443+
}
431444
private:
432445
explicit thread(std::function<void ()> func, attr attributes = attr(),
433446
bool main = false, bool app = false);

0 commit comments

Comments
 (0)