about summary refs log tree commit diff homepage
path: root/lib/Support
diff options
context:
space:
mode:
authorJiri Slaby <jslaby@suse.cz>2016-08-01 19:12:26 +0200
committerJiri Slaby <jslaby@suse.cz>2016-08-02 10:57:02 +0200
commit47cd9b3030b2cda212209dd9e282274d0d7547f0 (patch)
treece722a48fb100698b5cb7fed6d68bacccd74d624 /lib/Support
parent66adc6374cc9b43db2beccec9ae0a547dc411eae (diff)
downloadklee-47cd9b3030b2cda212209dd9e282274d0d7547f0.tar.gz
MemoryUsage: fix GetTotalMallocUsage
The mallinfo() interface is ill-designed. It returns 'int' as occupied
memory. This means at most 2G. This causes troubles when capping the
memory to 3G by -max-memory=3000 for example.

We cannot fix the interface, but we can at least extend the space to
4G. So cast those 'int's to 'unsigned int's to avoid sign extension.
Then do the addition on 'size_t' to count on 64bit values (on 64 bit).

Apart from that, the original 'int' + 'int' led to overflow which is
undefined on 'signed int's in C.

Also, when klee is run under valgrind, generic.current_allocated_bytes
from gperftools does not touch the passed pointer and in that case, we
return garbage from GetTotalMallocUsage. So initialize 'value' to 0 to
avoid the problem.

And since GetNumericProperty accepts 'size_t', let's define 'value' as
such. It was 'uint64_t' previously and they differ on 32 bit.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Diffstat (limited to 'lib/Support')
-rw-r--r--lib/Support/MemoryUsage.cpp6
1 files changed, 3 insertions, 3 deletions
diff --git a/lib/Support/MemoryUsage.cpp b/lib/Support/MemoryUsage.cpp
index a9f4026d..d141593a 100644
--- a/lib/Support/MemoryUsage.cpp
+++ b/lib/Support/MemoryUsage.cpp
@@ -26,7 +26,7 @@ using namespace klee;
 
 size_t util::GetTotalMallocUsage() {
 #ifdef HAVE_GPERFTOOLS_MALLOC_EXTENSION_H
-  uint64_t value;
+  size_t value = 0;
   MallocExtension::instance()->GetNumericProperty(
       "generic.current_allocated_bytes", &value);
   return value;
@@ -36,9 +36,9 @@ size_t util::GetTotalMallocUsage() {
   // does not include mmap()'ed memory in mi.uordblks
   // but other implementations (e.g. tcmalloc) do.
 #if defined(__GLIBC__)
-  return mi.uordblks + mi.hblkhd;
+  return (size_t)(unsigned)mi.uordblks + (unsigned)mi.hblkhd;
 #else
-  return mi.uordblks;
+  return (unsigned)mi.uordblks;
 #endif
 
 #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)