sewardj | 5cf515f | 2004-06-26 20:10:35 +0000 | [diff] [blame] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 3 | /*--- begin main_util.h ---*/ |
sewardj | 5cf515f | 2004-06-26 20:10:35 +0000 | [diff] [blame] | 4 | /*---------------------------------------------------------------*/ |
| 5 | |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 6 | /* |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 9 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame] | 10 | Copyright (C) 2004-2017 OpenWorks LLP |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 11 | info@open-works.net |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 12 | |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 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. |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 17 | |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 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 |
sewardj | 7bd6ffe | 2005-08-03 16:07:36 +0000 | [diff] [blame] | 26 | 02110-1301, USA. |
| 27 | |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 28 | The GNU General Public License is contained in the file COPYING. |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 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. |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 34 | */ |
| 35 | |
sewardj | cef7d3e | 2009-07-02 12:21:59 +0000 | [diff] [blame] | 36 | #ifndef __VEX_MAIN_UTIL_H |
| 37 | #define __VEX_MAIN_UTIL_H |
sewardj | 5cf515f | 2004-06-26 20:10:35 +0000 | [diff] [blame] | 38 | |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 39 | #include "libvex_basictypes.h" |
sewardj | 5cf515f | 2004-06-26 20:10:35 +0000 | [diff] [blame] | 40 | |
| 41 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 42 | /* Misc. */ |
| 43 | |
| 44 | #define NULL ((void*)0) |
| 45 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame] | 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) |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 55 | |
florian | a0ef1de | 2014-12-29 22:18:58 +0000 | [diff] [blame] | 56 | #if !defined(offsetof) |
| 57 | # define offsetof(type,memb) ((SizeT)(HWord)&((type*)0)->memb) |
| 58 | #endif |
| 59 | |
florian | 1996df0 | 2015-03-28 18:48:20 +0000 | [diff] [blame] | 60 | // Poor man's static assert |
florian | a2cdc9f | 2015-04-04 18:43:11 +0000 | [diff] [blame] | 61 | #define STATIC_ASSERT(x) extern int vex__unused_array[(x) ? 1 : -1] \ |
| 62 | __attribute__((unused)) |
sewardj | 5cf515f | 2004-06-26 20:10:35 +0000 | [diff] [blame] | 63 | |
florian | 1996df0 | 2015-03-28 18:48:20 +0000 | [diff] [blame] | 64 | /* Stuff for panicking and assertion. */ |
sewardj | 5cf515f | 2004-06-26 20:10:35 +0000 | [diff] [blame] | 65 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 66 | #define vassert(expr) \ |
sewardj | 7ee9752 | 2011-05-09 21:45:04 +0000 | [diff] [blame] | 67 | ((void) (LIKELY(expr) ? 0 : \ |
florian | 1996df0 | 2015-03-28 18:48:20 +0000 | [diff] [blame] | 68 | (vex_assert_fail (#expr, \ |
sewardj | 5cf515f | 2004-06-26 20:10:35 +0000 | [diff] [blame] | 69 | __FILE__, __LINE__, \ |
| 70 | __PRETTY_FUNCTION__), 0))) |
| 71 | |
| 72 | __attribute__ ((__noreturn__)) |
sewardj | 5827784 | 2005-02-07 03:11:17 +0000 | [diff] [blame] | 73 | extern void vex_assert_fail ( const HChar* expr, const HChar* file, |
| 74 | Int line, const HChar* fn ); |
sewardj | 5cf515f | 2004-06-26 20:10:35 +0000 | [diff] [blame] | 75 | __attribute__ ((__noreturn__)) |
florian | 76714fd | 2012-09-13 20:21:42 +0000 | [diff] [blame] | 76 | extern void vpanic ( const HChar* str ); |
sewardj | 5cf515f | 2004-06-26 20:10:35 +0000 | [diff] [blame] | 77 | |
florian | eebdb2b | 2014-12-10 16:08:09 +0000 | [diff] [blame] | 78 | __attribute__ ((__noreturn__)) __attribute__ ((format (printf, 1, 2))) |
| 79 | extern void vfatal ( const HChar* format, ... ); |
| 80 | |
sewardj | 5cf515f | 2004-06-26 20:10:35 +0000 | [diff] [blame] | 81 | |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 82 | /* Printing */ |
| 83 | |
| 84 | __attribute__ ((format (printf, 1, 2))) |
florian | 76714fd | 2012-09-13 20:21:42 +0000 | [diff] [blame] | 85 | extern UInt vex_printf ( const HChar *format, ... ); |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 86 | |
sewardj | 41f43bc | 2004-07-08 14:23:22 +0000 | [diff] [blame] | 87 | __attribute__ ((format (printf, 2, 3))) |
florian | 76714fd | 2012-09-13 20:21:42 +0000 | [diff] [blame] | 88 | extern UInt vex_sprintf ( HChar* buf, const HChar *format, ... ); |
sewardj | 35421a3 | 2004-07-05 13:12:34 +0000 | [diff] [blame] | 89 | |
sewardj | 36ca513 | 2004-07-24 13:12:23 +0000 | [diff] [blame] | 90 | |
| 91 | /* String ops */ |
| 92 | |
sewardj | 5827784 | 2005-02-07 03:11:17 +0000 | [diff] [blame] | 93 | extern Bool vex_streq ( const HChar* s1, const HChar* s2 ); |
florian | 04fc6b1 | 2014-12-29 20:22:26 +0000 | [diff] [blame] | 94 | extern SizeT vex_strlen ( const HChar* str ); |
| 95 | extern void vex_bzero ( void* s, SizeT n ); |
sewardj | 36ca513 | 2004-07-24 13:12:23 +0000 | [diff] [blame] | 96 | |
| 97 | |
sewardj | d887b86 | 2005-01-17 18:34:34 +0000 | [diff] [blame] | 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 ); |
sewardj | 2d6b14a | 2005-11-23 04:25:07 +0000 | [diff] [blame] | 113 | extern void vexAllocSanityCheck ( void ); |
sewardj | d887b86 | 2005-01-17 18:34:34 +0000 | [diff] [blame] | 114 | |
sewardj | 2d6b14a | 2005-11-23 04:25:07 +0000 | [diff] [blame] | 115 | extern void vexSetAllocModeTEMP_and_clear ( void ); |
sewardj | d887b86 | 2005-01-17 18:34:34 +0000 | [diff] [blame] | 116 | |
florian | d8e3eca | 2015-03-13 12:46:49 +0000 | [diff] [blame] | 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 | |
| 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 | |
sewardj | 4872906 | 2015-07-07 12:41:33 +0000 | [diff] [blame] | 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 | |
sewardj | cef7d3e | 2009-07-02 12:21:59 +0000 | [diff] [blame] | 181 | #endif /* ndef __VEX_MAIN_UTIL_H */ |
sewardj | ac9af02 | 2004-07-05 01:15:34 +0000 | [diff] [blame] | 182 | |
| 183 | /*---------------------------------------------------------------*/ |
sewardj | cef7d3e | 2009-07-02 12:21:59 +0000 | [diff] [blame] | 184 | /*--- main_util.h ---*/ |
sewardj | ac9af02 | 2004-07-05 01:15:34 +0000 | [diff] [blame] | 185 | /*---------------------------------------------------------------*/ |