Skip to content

Commit

Permalink
add poisoning to Persistacks (but now parallel_for has a use-after-po…
Browse files Browse the repository at this point in the history
…ison)
  • Loading branch information
mratsim committed Dec 29, 2019
1 parent bbaf716 commit a393c62
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions weave/memory/persistacks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import
std/typetraits,
../config,
../instrumentation/contracts,
../instrumentation/[contracts, sanitizers],
../memory/allocs

type
Expand Down Expand Up @@ -56,6 +56,7 @@ type
# ------------------------------------------------------------------------------

proc delete*[N: static int8, T](ps: var Persistack[N, T]) =
unpoisonMemRegion(ps.rawMem, N * sizeof(T))
if not T.supportsCopyMem():
# T has custom destructors or ref objects
for i in 0 ..< N:
Expand All @@ -72,6 +73,7 @@ proc initialize*[N: static int8, T](ps: var Persistack[N, T]) =
for i in 0 ..< N:
ps.stack[i] = ps.rawMem[i].addr
ps.len = N
poisonMemRegion(ps.rawMem, N * sizeof(T))

func borrow*[N: static int8, T](ps: var Persistack[N, T]): ptr T {.inline.} =
## Provides a reference from the persistack
Expand All @@ -89,13 +91,15 @@ func borrow*[N: static int8, T](ps: var Persistack[N, T]): ptr T {.inline.} =

ps.len -= 1
result = move ps.stack[ps.len]
unpoisonMemRegion(result, sizeof(T))

func recycle*[N: static int8, T](ps: var Persistack[N, T], reference: sink(ptr T)) {.inline.} =
## Returns a reference to the persistack.
## ⚠️ The lender thread must be the one recycling the reference
preCondition:
ps.len < N


poisonMemRegion(reference, sizeof(T))
`=sink`(ps.stack[ps.len], reference)
ps.len += 1

Expand All @@ -106,6 +110,7 @@ func nowAvailable*[N: static int8, T](ps: var Persistack[N, T], index: SomeInteg

ps.stack[ps.len] = ps.rawMem[index].addr
ps.len += 1
poisonMemRegion(ps.rawMem[index].addr, sizeof(T))

func access*[N: static int8, T](ps: Persistack[N, T], index: SomeInteger): var T {.inline.} =
## Access the object at `index`.
Expand Down

0 comments on commit a393c62

Please sign in to comment.