Home | History | Annotate | Download | only in priv
      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-2015 OpenWorks LLP
     11       info (at) 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 #define LIKELY(x)       __builtin_expect(!!(x), 1)
     47 #define UNLIKELY(x)     __builtin_expect(!!(x), 0)
     48 
     49 #if !defined(offsetof)
     50 #   define offsetof(type,memb) ((SizeT)(HWord)&((type*)0)->memb)
     51 #endif
     52 
     53 // Poor man's static assert
     54 #define STATIC_ASSERT(x)  extern int vex__unused_array[(x) ? 1 : -1] \
     55                                      __attribute__((unused))
     56 
     57 /* Stuff for panicking and assertion. */
     58 
     59 #define vassert(expr)                                           \
     60   ((void) (LIKELY(expr) ? 0 :                                   \
     61            (vex_assert_fail (#expr,                             \
     62                              __FILE__, __LINE__,                \
     63                              __PRETTY_FUNCTION__), 0)))
     64 
     65 __attribute__ ((__noreturn__))
     66 extern void vex_assert_fail ( const HChar* expr, const HChar* file,
     67                               Int line, const HChar* fn );
     68 __attribute__ ((__noreturn__))
     69 extern void vpanic ( const HChar* str );
     70 
     71 __attribute__ ((__noreturn__)) __attribute__ ((format (printf, 1, 2)))
     72 extern void vfatal ( const HChar* format, ... );
     73 
     74 
     75 /* Printing */
     76 
     77 __attribute__ ((format (printf, 1, 2)))
     78 extern UInt vex_printf ( const HChar *format, ... );
     79 
     80 __attribute__ ((format (printf, 2, 3)))
     81 extern UInt vex_sprintf ( HChar* buf, const HChar *format, ... );
     82 
     83 
     84 /* String ops */
     85 
     86 extern Bool vex_streq ( const HChar* s1, const HChar* s2 );
     87 extern SizeT vex_strlen ( const HChar* str );
     88 extern void vex_bzero ( void* s, SizeT n );
     89 
     90 
     91 /* Storage management: clear the area, and allocate from it. */
     92 
     93 /* By default allocation occurs in the temporary area.  However, it is
     94    possible to switch to permanent area allocation if that's what you
     95    want.  Permanent area allocation is very limited, tho. */
     96 
     97 typedef
     98    enum {
     99       VexAllocModeTEMP,
    100       VexAllocModePERM
    101    }
    102    VexAllocMode;
    103 
    104 extern void         vexSetAllocMode ( VexAllocMode );
    105 extern VexAllocMode vexGetAllocMode ( void );
    106 extern void         vexAllocSanityCheck ( void );
    107 
    108 extern void vexSetAllocModeTEMP_and_clear ( void );
    109 
    110 /* Allocate in Vex's temporary allocation area.  Be careful with this.
    111    You can only call it inside an instrumentation or optimisation
    112    callback that you have previously specified in a call to
    113    LibVEX_Translate.  The storage allocated will only stay alive until
    114    translation of the current basic block is complete.
    115  */
    116 extern HChar* private_LibVEX_alloc_first;
    117 extern HChar* private_LibVEX_alloc_curr;
    118 extern HChar* private_LibVEX_alloc_last;
    119 extern void   private_LibVEX_alloc_OOM(void) __attribute__((noreturn));
    120 
    121 /* Allocated memory as returned by LibVEX_Alloc will be aligned on this
    122    boundary. */
    123 #define REQ_ALIGN 8
    124 
    125 static inline void* LibVEX_Alloc_inline ( SizeT nbytes )
    126 {
    127    struct align {
    128       char c;
    129       union {
    130          char c;
    131          short s;
    132          int i;
    133          long l;
    134          long long ll;
    135          float f;
    136          double d;
    137          /* long double is currently not used and would increase alignment
    138             unnecessarily. */
    139          /* long double ld; */
    140          void *pto;
    141          void (*ptf)(void);
    142       } x;
    143    };
    144 
    145    /* Make sure the compiler does no surprise us */
    146    vassert(offsetof(struct align,x) <= REQ_ALIGN);
    147 
    148 #if 0
    149   /* Nasty debugging hack, do not use. */
    150   return malloc(nbytes);
    151 #else
    152    HChar* curr;
    153    HChar* next;
    154    SizeT  ALIGN;
    155    ALIGN  = offsetof(struct align,x) - 1;
    156    nbytes = (nbytes + ALIGN) & ~ALIGN;
    157    curr   = private_LibVEX_alloc_curr;
    158    next   = curr + nbytes;
    159    if (next >= private_LibVEX_alloc_last)
    160       private_LibVEX_alloc_OOM();
    161    private_LibVEX_alloc_curr = next;
    162    return curr;
    163 #endif
    164 }
    165 
    166 /* Misaligned memory access support. */
    167 
    168 extern UInt  read_misaligned_UInt_LE  ( void* addr );
    169 extern ULong read_misaligned_ULong_LE ( void* addr );
    170 
    171 extern void  write_misaligned_UInt_LE  ( void* addr, UInt  w );
    172 extern void  write_misaligned_ULong_LE ( void* addr, ULong w );
    173 
    174 #endif /* ndef __VEX_MAIN_UTIL_H */
    175 
    176 /*---------------------------------------------------------------*/
    177 /*---                                             main_util.h ---*/
    178 /*---------------------------------------------------------------*/
    179