From a393c6207430558292c33006467ee3850aeb0e8d Mon Sep 17 00:00:00 2001 From: mMay Date: Sun, 29 Dec 2019 23:27:31 +0100 Subject: [PATCH] add poisoning to Persistacks (but now parallel_for has a use-after-poison) --- weave/memory/persistacks.nim | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/weave/memory/persistacks.nim b/weave/memory/persistacks.nim index c5fa05a..4794050 100644 --- a/weave/memory/persistacks.nim +++ b/weave/memory/persistacks.nim @@ -8,7 +8,7 @@ import std/typetraits, ../config, - ../instrumentation/contracts, + ../instrumentation/[contracts, sanitizers], ../memory/allocs type @@ -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: @@ -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 @@ -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 @@ -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`.