Skip to content

Commit a4ed387

Browse files
authored
Fix Module tracking across ownership transfer. (#494)
Since we use simple structs, the same handle results in the same object object, triggering user-after-free warnings.
1 parent 355a783 commit a4ed387

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/debug.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ const memcheck_enabled = parse(Bool, @load_preference("memcheck", "false"))
99

1010
const tracked_objects = Dict{Any,Any}()
1111

12-
function mark_alloc(obj::Any)
12+
function mark_alloc(obj::Any; allow_overwrite::Bool=false)
1313
@static if memcheck_enabled
1414
io = Core.stdout
1515
new_alloc_bt = backtrace()[2:end]
1616

17-
if haskey(tracked_objects, obj)
17+
if haskey(tracked_objects, obj) && !allow_overwrite
1818
old_alloc_bt, dispose_bt = tracked_objects[obj]
1919
if dispose_bt == nothing
2020
print("\nWARNING: An instance of $(typeof(obj)) was not properly disposed of, and a new allocation will overwrite it.")

src/executionengine/ts_module.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,12 @@ function ThreadSafeModule(mod::Module)
8989
# Module and a regular Context, only a method to create one from a Module
9090
# and a pre-existing TSContext, which isn't useful...
9191
# TODO: expose the other convenience method?
92-
# XXX: work around this by serializing/deserializing the module in the correct contex
92+
# XXX: work around this by serializing/deserializing in the correct context
9393
bitcode = convert(MemoryBuffer, mod)
94-
new_mod = context!(context(ts_context())) do
94+
dispose(mod)
95+
mod = context!(context(ts_context())) do
9596
parse(Module, bitcode)
9697
end
97-
dispose(mod)
98-
mod = new_mod
9998
end
10099
@assert context(mod) == context(ts_context())
101100

@@ -140,7 +139,7 @@ end
140139

141140
function tsm_callback(data::Ptr{Cvoid}, ref::API.LLVMModuleRef)
142141
cb = Base.unsafe_pointer_to_objref(data)::ThreadSafeModuleCallback
143-
mod = Module(ref)
142+
mod = mark_alloc(Module(ref); allow_overwrite=true)
144143
ctx = context(mod)
145144
activate(ctx)
146145
try
@@ -149,6 +148,7 @@ function tsm_callback(data::Ptr{Cvoid}, ref::API.LLVMModuleRef)
149148
msg = sprint(Base.display_error, err, Base.catch_backtrace())
150149
return API.LLVMCreateStringError(msg)
151150
finally
151+
mark_dispose(mod)
152152
deactivate(ctx)
153153
end
154154
return convert(API.LLVMErrorRef, C_NULL)

0 commit comments

Comments
 (0)