Skip to content

Commit

Permalink
pmu_x86: Fix bug where machine code gets GC'd
Browse files Browse the repository at this point in the history
Keep the arrays containing machine code alive by storing references to
them in a Lua table. Otherwise they will be garbage collected.

dynasm uses a GC callback to unmap memory that was used for generated
code and so the most likely consequence is a segfault. Here is how it
looks in dmesg:

  segfault at 7fe0d50de000 ip 00007fe0d50de000 sp 00007ffcd2c89cb8 error 14

where "error 14" means an error during instruction fetch.

This problem triggered immediately when using the pmu library with
non-trivial code under test (running an app network).
  • Loading branch information
lukego committed Sep 13, 2015
1 parent 6c0545d commit ee0ebbe
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lib/pmu_x86.dasl
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ local dasm = require("dasm")

local gen = {}

-- Table keeping machine code alive to the GC.
local anchor = {}

-- Utility: assemble code and optionally dump disassembly.
function assemble (name, prototype, generator)
local Dst = dasm.new(actions)
generator(Dst)
local mcode, size = Dst:build()
table.insert(anchor, mcode)
if debug then
print("mcode dump: "..name)
dasm.dump(mcode, size)
Expand Down

0 comments on commit ee0ebbe

Please sign in to comment.