Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
infrastructure.cpp
Go to the documentation of this file.
1 
10 #include <cassert>
11 #include <climits>
12 #include <iostream>
14 #include <stdexcept>
15 #ifdef __linux__
16 #include <unistd.h>
17 #endif
18 #ifdef __GLIBC__
19 #include <execinfo.h> // backtraces
20 #endif
21 
22 namespace gadgetlib2
23 {
24 
25 /********************************************************/
26 /*************** Debug String Formatting ****************/
27 /********************************************************/
28 
29 #ifdef DEBUG
30 const static size_t MAX_FMT = 256;
31 ::std::string GADGETLIB2_FMT(const char *format, ...)
32 {
33  char buf[MAX_FMT];
34  va_list args;
35  va_start(args, format);
36 #if defined(_MSC_VER)
37  const int strChk = vsnprintf_s(buf, MAX_FMT, MAX_FMT, format, args);
38 #else
39  const int strChk = vsnprintf(buf, MAX_FMT, format, args);
40 #endif
41  va_end(args);
43  strChk >= 0 && (size_t)strChk < MAX_FMT,
44  "String length larger than buffer. Shorten"
45  " string or increase buffer size defined in \"MAX_FMT\".");
46  return ::std::string(buf);
47 }
48 #else
49 ::std::string GADGETLIB2_FMT(const char *format, ...)
50 {
51  libff::UNUSED(format);
52  return "";
53 }
54 #endif
55 
57 long safeConvert(const int64_t num)
58 {
59  assert(num <= INT_MAX && num >= INT_MIN);
60  return (long)num;
61 }
62 
63 /*****************************************************************************/
64 /*********************** Error Handling *************************************/
65 /*****************************************************************************/
66 
67 /*
68  TODO add dumping of environment variables and run command to a log file and
69  add log file path to release mode error message. We don't want people running
70  release version to get any internal information (variable values, stack
71  trace, etc.) but want to have every data possible to reproduce assertion.
72 */
73 void ErrorHandling::fatalError(const ::std::string &msg)
74 {
75 #ifdef DEBUG
76  ::std::cerr << "ERROR: " << msg << ::std::endl << ::std::endl;
78  throw ::std::runtime_error(msg);
79 #else
80  libff::UNUSED(msg);
81  const ::std::string releaseMsg(
82  "Fatal error encountered. Run debug build for more"
83  " information and stack trace.");
84  ::std::cerr << "ERROR: " << releaseMsg << ::std::endl << ::std::endl;
85  throw ::std::runtime_error(releaseMsg);
86 #endif
87 }
88 
89 void ErrorHandling::fatalError(const ::std::stringstream &msg)
90 {
91  fatalError(msg.str());
92 }
93 
95 {
96 #ifdef __GLIBC__
97  std::cerr << "Stack trace (pipe through c++filt to demangle identifiers):"
98  << std::endl;
99  const int maxFrames = 100;
100  void *frames[maxFrames];
101  // Fill array with pointers to stack frames
102  int numFrames = backtrace(frames, maxFrames);
103  // Decode frames and print them to stderr
104  backtrace_symbols_fd(frames, numFrames, STDERR_FILENO);
105 #else
106  // TODO make this available for non-glibc platforms (e.g. musl libc on Linux
107  // and Windows)
108  std::cerr << " (stack trace not available on this platform)" << std::endl;
109 #endif // __GNUC__
110 }
111 
112 /*****************************************************************************/
113 /**************************** Basic Math ***********************************/
114 /*****************************************************************************/
115 
116 double Log2(double n) { return log(n) / log((double)2); }
117 
120 unsigned int Log2ceil(uint64_t i)
121 {
122  int retval = i ? 1 : 0;
123  while (i >>= 1) {
124  ++retval;
125  }
126  return retval;
127 }
128 
130 bool IsPower2(const long x) { return ((x > 0) && ((x & (x - 1)) == 0)); }
131 
132 } // namespace gadgetlib2
gadgetlib2::Log2
double Log2(double n)
Definition: infrastructure.cpp:116
gadgetlib2::GADGETLIB2_FMT
::std::string GADGETLIB2_FMT(const char *format,...)
Definition: infrastructure.cpp:49
gadgetlib2::ErrorHandling::fatalError
static void __noreturn fatalError(const ::std::string &msg)
Definition: infrastructure.cpp:73
infrastructure.hpp
gadgetlib2::safeConvert
long safeConvert(const int64_t num)
Definition: infrastructure.cpp:57
gadgetlib2::ErrorHandling::printStacktrace
static void printStacktrace()
Definition: infrastructure.cpp:94
gadgetlib2::Log2ceil
unsigned int Log2ceil(uint64_t i)
Definition: infrastructure.cpp:120
GADGETLIB_ASSERT
#define GADGETLIB_ASSERT(predicate, msg)
Definition: infrastructure.hpp:94
gadgetlib2::IsPower2
bool IsPower2(const long x)
Returns true iff x is a power of 2.
Definition: infrastructure.cpp:130
gadgetlib2
Definition: adapters.cpp:15