Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add travis integration and fix the build #2

Closed
wants to merge 12 commits into from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,5 @@ depsrc/
depinst/

Makefile.*
zerocash_pour_ppzksnark/profiling/profile_zerocash_pour_gadget
zerocash_pour_ppzksnark/tests/test_zerocash_pour_ppzksnark
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a third test binary somewhere. I can't remember where, though.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me too, wasn't able to find it. I'll double check afterward.

15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
os: linux
language: cpp
compiler: gcc

install:
- sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y # For g++-4.9
- sudo apt-get update -qq
- sudo apt-get install -qq g++-4.9
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 90
- sudo apt-get install -qq libgmp-dev libssl-dev libgtest-dev libcrypto++-dev libboost-dev libboost-program-options-dev

script:
- CXXFLAGS="-lrt" LIBSNARK_FLAGS="NO_PROCPS=1 NO_GTEST=1 NO_DOCS=1 STATIC=1" ./get-libsnark
- NO_PROCPS=1 MINDEPS=1 make
- ./zerocash_pour_ppzksnark/tests/test_zerocash_pour_ppzksnark
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ LIBZEROCASH=libzerocash
UTILS=$(LIBZEROCASH)/utils
TESTUTILS=tests
LDLIBS += -L $(DEPINST)/lib -Wl,-rpath $(DEPINST)/lib -L . -lsnark -lzm -lgmpxx -lgmp
LDLIBS += -lboost_system-mt -lcrypto -lcryptopp
ifeq ($(USE_MT),1)
LDLIBS += -lboost_system-mt
else
LDLIBS += -lboost_system
endif

LDLIBS += -lcrypto -lcryptopp
CXXFLAGS += -I $(DEPINST)/include -I $(DEPINST)/include/libsnark -I . -DUSE_ASM -DCURVE_ALT_BN128

LIBPATH = /usr/local/lib
Expand Down
97 changes: 55 additions & 42 deletions libzerocash/allocators.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#include <map>
#include <string>
#include <string.h>
#include <vector>

#include <boost/thread/mutex.hpp>
#include <boost/thread/once.hpp>

#include <openssl/crypto.h> // for OPENSSL_cleanse()

/**
Expand All @@ -25,14 +27,14 @@
* small objects that span up to a few pages, mostly smaller than a page. To support large allocations,
* something like an interval tree would be the preferred data structure.
*/
template <class Locker> class LockedPageManagerBase
template <class Locker>
class LockedPageManagerBase
{
public:
LockedPageManagerBase(size_t page_size):
page_size(page_size)
LockedPageManagerBase(size_t page_size) : page_size(page_size)
{
// Determine bitmask for extracting page from address
assert(!(page_size & (page_size-1))); // size must be power of two
assert(!(page_size & (page_size - 1))); // size must be power of two
page_mask = ~(page_size - 1);
}

Expand All @@ -43,43 +45,42 @@ template <class Locker> class LockedPageManagerBase


// For all pages in affected range, increase lock count
void LockRange(void *p, size_t size)
void LockRange(void* p, size_t size)
{
boost::mutex::scoped_lock lock(mutex);
if(!size) return;
if (!size)
return;
const size_t base_addr = reinterpret_cast<size_t>(p);
const size_t start_page = base_addr & page_mask;
const size_t end_page = (base_addr + size - 1) & page_mask;
for(size_t page = start_page; page <= end_page; page += page_size)
{
for (size_t page = start_page; page <= end_page; page += page_size) {
Histogram::iterator it = histogram.find(page);
if(it == histogram.end()) // Newly locked page
if (it == histogram.end()) // Newly locked page
{
locker.Lock(reinterpret_cast<void*>(page), page_size);
histogram.insert(std::make_pair(page, 1));
}
else // Page was already locked; increase counter
} else // Page was already locked; increase counter
{
it->second += 1;
}
}
}

// For all pages in affected range, decrease lock count
void UnlockRange(void *p, size_t size)
void UnlockRange(void* p, size_t size)
{
boost::mutex::scoped_lock lock(mutex);
if(!size) return;
if (!size)
return;
const size_t base_addr = reinterpret_cast<size_t>(p);
const size_t start_page = base_addr & page_mask;
const size_t end_page = (base_addr + size - 1) & page_mask;
for(size_t page = start_page; page <= end_page; page += page_size)
{
for (size_t page = start_page; page <= end_page; page += page_size) {
Histogram::iterator it = histogram.find(page);
assert(it != histogram.end()); // Cannot unlock an area that was not locked
// Decrease counter for page, when it is zero, the page will be unlocked
it->second -= 1;
if(it->second == 0) // Nothing on the page anymore that keeps it locked
if (it->second == 0) // Nothing on the page anymore that keeps it locked
{
// Unlock page and remove the count from histogram
locker.Unlock(reinterpret_cast<void*>(page), page_size);
Expand All @@ -100,7 +101,7 @@ template <class Locker> class LockedPageManagerBase
boost::mutex mutex;
size_t page_size, page_mask;
// map of page base address to lock count
typedef std::map<size_t,int> Histogram;
typedef std::map<size_t, int> Histogram;
Histogram histogram;
};

Expand All @@ -115,11 +116,11 @@ class MemoryPageLocker
/** Lock memory pages.
* addr and len must be a multiple of the system page size
*/
bool Lock(const void *addr, size_t len);
bool Lock(const void* addr, size_t len);
/** Unlock memory pages.
* addr and len must be a multiple of the system page size
*/
bool Unlock(const void *addr, size_t len);
bool Unlock(const void* addr, size_t len);
};

/**
Expand All @@ -131,9 +132,9 @@ class MemoryPageLocker
* Due to the unpredictable order of static initializers, we have to make sure the
* LockedPageManager instance exists before any other STL-based objects that use
* secure_allocator are created. So instead of having LockedPageManager also be
* static-intialized, it is created on demand.
* static-initialized, it is created on demand.
*/
class LockedPageManager: public LockedPageManagerBase<MemoryPageLocker>
class LockedPageManager : public LockedPageManagerBase<MemoryPageLocker>
{
public:
static LockedPageManager& Instance()
Expand Down Expand Up @@ -164,11 +165,15 @@ class LockedPageManager: public LockedPageManagerBase<MemoryPageLocker>
// Functions for directly locking/unlocking memory objects.
// Intended for non-dynamically allocated structures.
//
template<typename T> void LockObject(const T &t) {
template <typename T>
void LockObject(const T& t)
{
LockedPageManager::Instance().LockRange((void*)(&t), sizeof(T));
}

template<typename T> void UnlockObject(const T &t) {
template <typename T>
void UnlockObject(const T& t)
{
OPENSSL_cleanse((void*)(&t), sizeof(T));
LockedPageManager::Instance().UnlockRange((void*)(&t), sizeof(T));
}
Expand All @@ -177,13 +182,12 @@ template<typename T> void UnlockObject(const T &t) {
// Allocator that locks its contents from being paged
// out of memory and clears its contents before deletion.
//
template<typename T>
struct secure_allocator : public std::allocator<T>
{
template <typename T>
struct secure_allocator : public std::allocator<T> {
// MSVC8 default copy constructor is broken
typedef std::allocator<T> base;
typedef typename base::size_type size_type;
typedef typename base::difference_type difference_type;
typedef typename base::difference_type difference_type;
typedef typename base::pointer pointer;
typedef typename base::const_pointer const_pointer;
typedef typename base::reference reference;
Expand All @@ -192,14 +196,18 @@ struct secure_allocator : public std::allocator<T>
secure_allocator() throw() {}
secure_allocator(const secure_allocator& a) throw() : base(a) {}
template <typename U>
secure_allocator(const secure_allocator<U>& a) throw() : base(a) {}
secure_allocator(const secure_allocator<U>& a) throw() : base(a)
{
}
~secure_allocator() throw() {}
template<typename _Other> struct rebind
{ typedef secure_allocator<_Other> other; };
template <typename _Other>
struct rebind {
typedef secure_allocator<_Other> other;
};

T* allocate(std::size_t n, const void *hint = 0)
T* allocate(std::size_t n, const void* hint = 0)
{
T *p;
T* p;
p = std::allocator<T>::allocate(n, hint);
if (p != NULL)
LockedPageManager::Instance().LockRange(p, sizeof(T) * n);
Expand All @@ -208,8 +216,7 @@ struct secure_allocator : public std::allocator<T>

void deallocate(T* p, std::size_t n)
{
if (p != NULL)
{
if (p != NULL) {
OPENSSL_cleanse(p, sizeof(T) * n);
LockedPageManager::Instance().UnlockRange(p, sizeof(T) * n);
}
Expand All @@ -221,13 +228,12 @@ struct secure_allocator : public std::allocator<T>
//
// Allocator that clears its contents before deletion.
//
template<typename T>
struct zero_after_free_allocator : public std::allocator<T>
{
template <typename T>
struct zero_after_free_allocator : public std::allocator<T> {
// MSVC8 default copy constructor is broken
typedef std::allocator<T> base;
typedef typename base::size_type size_type;
typedef typename base::difference_type difference_type;
typedef typename base::difference_type difference_type;
typedef typename base::pointer pointer;
typedef typename base::const_pointer const_pointer;
typedef typename base::reference reference;
Expand All @@ -236,10 +242,14 @@ struct zero_after_free_allocator : public std::allocator<T>
zero_after_free_allocator() throw() {}
zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {}
template <typename U>
zero_after_free_allocator(const zero_after_free_allocator<U>& a) throw() : base(a) {}
zero_after_free_allocator(const zero_after_free_allocator<U>& a) throw() : base(a)
{
}
~zero_after_free_allocator() throw() {}
template<typename _Other> struct rebind
{ typedef zero_after_free_allocator<_Other> other; };
template <typename _Other>
struct rebind {
typedef zero_after_free_allocator<_Other> other;
};

void deallocate(T* p, std::size_t n)
{
Expand All @@ -252,4 +262,7 @@ struct zero_after_free_allocator : public std::allocator<T>
// This is exactly like std::string, but with a custom allocator.
typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;

#endif
// Byte-vector that clears its contents before deletion.
typedef std::vector<char, zero_after_free_allocator<char> > CSerializeData;

#endif // BITCOIN_ALLOCATORS_H
Loading