1
2 /*---------------------------------------------------------------*/
3 /*--- begin main_util.h ---*/
4 /*---------------------------------------------------------------*/
5
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2004-2017 OpenWorks LLP
11 info@open-works.net
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 02110-1301, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29
30 Neither the names of the U.S. Department of Energy nor the
31 University of California nor the names of its contributors may be
32 used to endorse or promote products derived from this software
33 without prior written permission.
34 */
35
36 #ifndef __VEX_MAIN_UTIL_H
37 #define __VEX_MAIN_UTIL_H
38
39 #include "libvex_basictypes.h"
40
41
42 /* Misc. */
43
44 #define NULL ((void*)0)
45
46 #if defined(_MSC_VER) // building with MSVC
47 # define LIKELY(x) (x)
48 # define UNLIKELY(x) (x)
49 # define CAST_TO_TYPEOF(x) /**/
50 #else
51 # define LIKELY(x) __builtin_expect(!!(x), 1)
52 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
53 # define CAST_TO_TYPEOF(x) (__typeof__(x))
54 #endif // defined(_MSC_VER)
55
56 #if !defined(offsetof)
57 # define offsetof(type,memb) ((SizeT)(HWord)&((type*)0)->memb)
58 #endif
59
60 // Poor man's static assert
61 #define STATIC_ASSERT(x) extern int vex__unused_array[(x) ? 1 : -1] \
62 __attribute__((unused))
63
64 /* Stuff for panicking and assertion. */
65
66 #define vassert(expr) \
67 ((void) (LIKELY(expr) ? 0 : \
68 (vex_assert_fail (#expr, \
69 __FILE__, __LINE__, \
70 __PRETTY_FUNCTION__), 0)))
71
72 __attribute__ ((__noreturn__))
73 extern void vex_assert_fail ( const HChar* expr, const HChar* file,
74 Int line, const HChar* fn );
75 __attribute__ ((__noreturn__))
76 extern void vpanic ( const HChar* str );
77
78 __attribute__ ((__noreturn__)) __attribute__ ((format (printf, 1, 2)))
79 extern void vfatal ( const HChar* format, ... );
80
81
82 /* Printing */
83
84 __attribute__ ((format (printf, 1, 2)))
85 extern UInt vex_printf ( const HChar *format, ... );
86
87 __attribute__ ((format (printf, 2, 3)))
88 extern UInt vex_sprintf ( HChar* buf, const HChar *format, ... );
89
90
91 /* String ops */
92
93 extern Bool vex_streq ( const HChar* s1, const HChar* s2 );
94 extern SizeT vex_strlen ( const HChar* str );
95 extern void vex_bzero ( void* s, SizeT n );
96
97
98 /* Storage management: clear the area, and allocate from it. */
99
100 /* By default allocation occurs in the temporary area. However, it is
101 possible to switch to permanent area allocation if that's what you
102 want. Permanent area allocation is very limited, tho. */
103
104 typedef
105 enum {
106 VexAllocModeTEMP,
107 VexAllocModePERM
108 }
109 VexAllocMode;
110
111 extern void vexSetAllocMode ( VexAllocMode );
112 extern VexAllocMode vexGetAllocMode ( void );
113 extern void vexAllocSanityCheck ( void );
114
115 extern void vexSetAllocModeTEMP_and_clear ( void );
116
117 /* Allocate in Vex's temporary allocation area. Be careful with this.
118 You can only call it inside an instrumentation or optimisation
119 callback that you have previously specified in a call to
120 LibVEX_Translate. The storage allocated will only stay alive until
121 translation of the current basic block is complete.
122 */
123 extern HChar* private_LibVEX_alloc_first;
124 extern HChar* private_LibVEX_alloc_curr;
125 extern HChar* private_LibVEX_alloc_last;
126 extern void private_LibVEX_alloc_OOM(void) __attribute__((noreturn));
127
128 /* Allocated memory as returned by LibVEX_Alloc will be aligned on this
129 boundary. */
130 #define REQ_ALIGN 8
131
LibVEX_Alloc_inline(SizeT nbytes)132 static inline void* LibVEX_Alloc_inline ( SizeT nbytes )
133 {
134 struct align {
135 char c;
136 union {
137 char c;
138 short s;
139 int i;
140 long l;
141 long long ll;
142 float f;
143 double d;
144 /* long double is currently not used and would increase alignment
145 unnecessarily. */
146 /* long double ld; */
147 void *pto;
148 void (*ptf)(void);
149 } x;
150 };
151
152 /* Make sure the compiler does no surprise us */
153 vassert(offsetof(struct align,x) <= REQ_ALIGN);
154
155 #if 0
156 /* Nasty debugging hack, do not use. */
157 return malloc(nbytes);
158 #else
159 HChar* curr;
160 HChar* next;
161 SizeT ALIGN;
162 ALIGN = offsetof(struct align,x) - 1;
163 nbytes = (nbytes + ALIGN) & ~ALIGN;
164 curr = private_LibVEX_alloc_curr;
165 next = curr + nbytes;
166 if (next >= private_LibVEX_alloc_last)
167 private_LibVEX_alloc_OOM();
168 private_LibVEX_alloc_curr = next;
169 return curr;
170 #endif
171 }
172
173 /* Misaligned memory access support. */
174
175 extern UInt read_misaligned_UInt_LE ( void* addr );
176 extern ULong read_misaligned_ULong_LE ( void* addr );
177
178 extern void write_misaligned_UInt_LE ( void* addr, UInt w );
179 extern void write_misaligned_ULong_LE ( void* addr, ULong w );
180
181 #endif /* ndef __VEX_MAIN_UTIL_H */
182
183 /*---------------------------------------------------------------*/
184 /*--- main_util.h ---*/
185 /*---------------------------------------------------------------*/
186