From 8773fd5897d1057dc8f87d4ffd13c96a708080e8 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Sun, 28 Dec 2014 21:27:51 +0100 Subject: [PATCH] Added memhasher (yosys -M) --- kernel/driver.cc | 11 +++++++++-- kernel/rtlil.cc | 3 +++ kernel/yosys.cc | 37 +++++++++++++++++++++++++++++++++++++ kernel/yosys.h | 7 +++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/kernel/driver.cc b/kernel/driver.cc index 4700bf61b..9a81d8a48 100644 --- a/kernel/driver.cc +++ b/kernel/driver.cc @@ -151,8 +151,11 @@ int main(int argc, char **argv) printf(" -m module_file\n"); printf(" load the specified module (aka plugin)\n"); printf("\n"); + printf(" -M\n"); + printf(" will slightly randomize allocated pointer addresses. for debugging\n"); + printf("\n"); printf(" -A\n"); - printf(" will call abort() at the end of the script. useful for debugging\n"); + printf(" will call abort() at the end of the script. for debugging\n"); printf("\n"); printf(" -V\n"); printf(" print version information and exit\n"); @@ -174,10 +177,13 @@ int main(int argc, char **argv) } int opt; - while ((opt = getopt(argc, argv, "AQTVSm:f:Hh:b:o:p:l:qv:tds:c:")) != -1) + while ((opt = getopt(argc, argv, "MAQTVSm:f:Hh:b:o:p:l:qv:tds:c:")) != -1) { switch (opt) { + case 'M': + memhasher_on(); + break; case 'A': call_abort = true; break; @@ -407,6 +413,7 @@ int main(int argc, char **argv) } #endif + memhasher_off(); if (call_abort) abort(); diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 6f2d367d6..cdbaa5bbf 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -1766,6 +1766,9 @@ RTLIL::Cell::Cell() : module(nullptr) { static unsigned int hashidx_count = 0; hashidx_ = hashidx_count++; + + // log("#memtrace# %p\n", this); + memhasher(); } bool RTLIL::Cell::hasPort(RTLIL::IdString portname) const diff --git a/kernel/yosys.cc b/kernel/yosys.cc index 6cc643152..52bd066b7 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -55,6 +55,43 @@ RTLIL::Design *yosys_design = NULL; Tcl_Interp *yosys_tcl_interp = NULL; #endif +bool memhasher_active = false; +uint32_t memhasher_rng; +std::vector memhasher_store; + +void memhasher_on() +{ + memhasher_rng += time(NULL) << 16 ^ getpid(); + memhasher_store.resize(0x10000); + memhasher_active = true; +} + +void memhasher_off() +{ + for (auto p : memhasher_store) + if (p) free(p); + memhasher_store.clear(); + memhasher_active = false; +} + +void memhasher_do() +{ + memhasher_rng ^= memhasher_rng << 13; + memhasher_rng ^= memhasher_rng >> 17; + memhasher_rng ^= memhasher_rng << 5; + + int size, index = (memhasher_rng >> 4) & 0xffff; + switch (memhasher_rng & 7) { + case 0: size = 16; break; + case 1: size = 256; break; + case 2: size = 1024; break; + case 3: size = 4096; break; + default: size = 0; + } + if (index < 16) size *= 16; + memhasher_store[index] = realloc(memhasher_store[index], size); +} + std::string stringf(const char *fmt, ...) { std::string string; diff --git a/kernel/yosys.h b/kernel/yosys.h index b653941ce..50a159939 100644 --- a/kernel/yosys.h +++ b/kernel/yosys.h @@ -169,6 +169,13 @@ template<> struct hash_ops : hash_obj_ops {}; template<> struct hash_ops : hash_obj_ops {}; template<> struct hash_ops : hash_obj_ops {}; +void memhasher_on(); +void memhasher_off(); +void memhasher_do(); + +extern bool memhasher_active; +inline void memhasher() { if (memhasher_active) memhasher_do(); } + std::string stringf(const char *fmt, ...) YS_ATTRIBUTE(format(printf, 1, 2)); std::string vstringf(const char *fmt, va_list ap); int readsome(std::istream &f, char *s, int n); -- 2.30.2