Merge pull request #3713 from lethosor/fix-removejob-gcc-optimization

Hack to force GCC to invoke cancel_job() through DF's vtable
develop
Myk 2023-08-31 15:27:56 -07:00 committed by GitHub
commit 75df1c0426
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

@ -387,7 +387,14 @@ bool DFHack::Job::removeJob(df::job* job) {
// call the job cancel vmethod graciously provided by The Toady One.
// job_handler::cancel_job calls job::~job, and then deletes job (this has
// been confirmed by disassembly).
world->jobs.cancel_job(job);
// HACK: GCC (starting around GCC 10 targeting C++20 as of v50.09) optimizes
// out the vmethod call here regardless of optimization level, so we need to
// invoke the vmethod manually through a pointer, as the Lua wrapper does.
// `volatile` does not seem to be necessary but is included for good
// measure.
volatile auto cancel_job_method = &df::job_handler::cancel_job;
(world->jobs.*cancel_job_method)(job);
return true;
}

@ -0,0 +1,19 @@
config.target = 'core'
config.mode = 'title' -- alters world state, not safe when a world is loaded
function test.removeJob()
-- removeJob() calls DF code, so ensure that that DF code is actually running
-- for an explanation of why this is necessary to check,
-- see https://github.com/DFHack/dfhack/pull/3713 and Job.cpp:removeJob()
expect.nil_(df.global.world.jobs.list.next, 'job list is not empty')
local job = df.job:new() -- will be deleted by removeJob() if the test passes
dfhack.job.linkIntoWorld(job)
expect.true_(df.global.world.jobs.list.next, 'job list is empty')
expect.eq(df.global.world.jobs.list.next.item, job, 'expected job not found in list')
expect.true_(dfhack.job.removeJob(job))
expect.nil_(df.global.world.jobs.list.next, 'job list is not empty after removeJob()')
end