aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@digia.com>2013-03-12 14:49:13 -0400
committerSimon Hausmann <simon.hausmann@digia.com>2013-03-13 07:04:42 +0100
commitd24239d1ff5b13a18367620d6ae4181c4f299ec3 (patch)
tree4503856c2c124dacd7f11fa190d5bb508fe08f7c /src
parentddaa93d83cd8216cae68c0575f72ce11bd3160ed (diff)
Add valgrind support to our garbage collector
Tell valgrind about the memory we allocate and free in the garbage collector. Also mark stack variables as initialized before walking the stack. This avoids all valgrind warnings related to GC and gives proper warnings if we should attempt to access garbage collected memory. Change-Id: I7e923a163c25e7a5b4409b1b9c2191f314675f2d Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'src')
-rw-r--r--src/v4/qv4managed.cpp2
-rw-r--r--src/v4/qv4mm.cpp36
-rw-r--r--src/v4/v4.pro4
3 files changed, 40 insertions, 2 deletions
diff --git a/src/v4/qv4managed.cpp b/src/v4/qv4managed.cpp
index 30219b9afe..ab87b9dd9f 100644
--- a/src/v4/qv4managed.cpp
+++ b/src/v4/qv4managed.cpp
@@ -77,6 +77,8 @@ void Managed::operator delete(void *ptr)
return;
Managed *m = static_cast<Managed *>(ptr);
+ m->vtbl = 0;
+ m->_data = 0;
m->~Managed();
}
diff --git a/src/v4/qv4mm.cpp b/src/v4/qv4mm.cpp
index 8c62d64a74..c43bb3e369 100644
--- a/src/v4/qv4mm.cpp
+++ b/src/v4/qv4mm.cpp
@@ -43,6 +43,11 @@
#include <cstdlib>
#include "qv4alloca_p.h"
+#ifdef V4_USE_VALGRIND
+#include <valgrind/valgrind.h>
+#include <valgrind/memcheck.h>
+#endif
+
using namespace QQmlJS::VM;
using namespace WTF;
@@ -112,6 +117,9 @@ MemoryManager::MemoryManager()
: m_d(new Data(true))
{
setEnableGC(true);
+#ifdef V4_USE_VALGRIND
+ VALGRIND_CREATE_MEMPOOL(this, 0, true);
+#endif
}
Managed *MemoryManager::alloc(std::size_t size)
@@ -170,9 +178,16 @@ Managed *MemoryManager::alloc(std::size_t size)
*last = 0;
m = m_d->smallItems[pos];
m_d->availableItems[pos] += allocation.memory.size()/size - 1;
+#ifdef V4_USE_VALGRIND
+ VALGRIND_MAKE_MEM_NOACCESS(allocation.memory, allocation.chunkSize);
+#endif
}
found:
+#ifdef V4_USE_VALGRIND
+ VALGRIND_MEMPOOL_ALLOC(this, m, size);
+#endif
+
m_d->smallItems[pos] = m->nextFree();
return m;
}
@@ -234,6 +249,9 @@ std::size_t MemoryManager::sweep(char *chunkStart, std::size_t chunkSize, size_t
Managed **f = &m_d->smallItems[size >> 4];
+#ifdef V4_USE_VALGRIND
+ VALGRIND_DISABLE_ERROR_REPORTING;
+#endif
for (char *chunk = chunkStart, *chunkEnd = chunk + chunkSize - size; chunk <= chunkEnd; chunk += size) {
Managed *m = reinterpret_cast<Managed *>(chunk);
// qDebug("chunk @ %p, size = %lu, in use: %s, mark bit: %s",
@@ -246,15 +264,25 @@ std::size_t MemoryManager::sweep(char *chunkStart, std::size_t chunkSize, size_t
m->markBit = 0;
} else {
// qDebug() << "-- collecting it." << m << *f << m->nextFree();
+#ifdef V4_USE_VALGRIND
+ VALGRIND_ENABLE_ERROR_REPORTING;
+#endif
m->vtbl->destroy(m);
m->setNextFree(*f);
+#ifdef V4_USE_VALGRIND
+ VALGRIND_DISABLE_ERROR_REPORTING;
+ VALGRIND_MEMPOOL_FREE(this, m);
+#endif
*f = m;
SCRIBBLE(m, 0x99, size);
++freedCount;
}
}
}
+#ifdef V4_USE_VALGRIND
+ VALGRIND_ENABLE_ERROR_REPORTING;
+#endif
return freedCount;
}
@@ -358,11 +386,11 @@ void MemoryManager::willAllocate(std::size_t size)
void MemoryManager::collectFromStack() const
{
+ quintptr valueOnStack = 0;
+
if (!m_d->heapChunks.count())
return;
- quintptr valueOnStack = 0;
-
#if USE(PTHREADS)
# if OS(DARWIN)
void* stackTop = 0;
@@ -392,6 +420,10 @@ void MemoryManager::collectFromStack() const
quintptr *current = (&valueOnStack) + 1;
// qDebug() << "collectFromStack";// << top << current << &valueOnStack;
+#if V4_USE_VALGRIND
+ VALGRIND_MAKE_MEM_DEFINED(current, (top - current)*sizeof(quintptr));
+#endif
+
char** heapChunkBoundaries = (char**)alloca(m_d->heapChunks.count() * 2 * sizeof(char*));
char** heapChunkBoundariesEnd = heapChunkBoundaries + 2 * m_d->heapChunks.count();
int i = 0;
diff --git a/src/v4/v4.pro b/src/v4/v4.pro
index 447df2fc7c..80444b89fa 100644
--- a/src/v4/v4.pro
+++ b/src/v4/v4.pro
@@ -163,6 +163,10 @@ debug-with-libunwind {
DEFINES += WTF_USE_LIBUNWIND_DEBUG=1
}
+valgrind {
+ DEFINES += V4_USE_VALGRIND
+}
+
include(moth/moth.pri)
include(../3rdparty/masm/masm.pri)
include(../3rdparty/double-conversion/double-conversion.pri)