types.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. american fuzzy lop - type definitions and minor macros
  3. ------------------------------------------------------
  4. Written and maintained by Michal Zalewski <lcamtuf@google.com>
  5. Copyright 2013, 2014, 2015 Google Inc. All rights reserved.
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at:
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. */
  11. #ifndef _HAVE_TYPES_H
  12. #define _HAVE_TYPES_H
  13. #include <stdint.h>
  14. #include <stdlib.h>
  15. typedef uint8_t u8;
  16. typedef uint16_t u16;
  17. typedef uint32_t u32;
  18. /*
  19. Ugh. There is an unintended compiler / glibc #include glitch caused by
  20. combining the u64 type an %llu in format strings, necessitating a workaround.
  21. In essence, the compiler is always looking for 'unsigned long long' for %llu.
  22. On 32-bit systems, the u64 type (aliased to uint64_t) is expanded to
  23. 'unsigned long long' in <bits/types.h>, so everything checks out.
  24. But on 64-bit systems, it is #ifdef'ed in the same file as 'unsigned long'.
  25. Now, it only happens in circumstances where the type happens to have the
  26. expected bit width, *but* the compiler does not know that... and complains
  27. about 'unsigned long' being unsafe to pass to %llu.
  28. */
  29. #ifdef __x86_64__
  30. typedef unsigned long long u64;
  31. #else
  32. typedef uint64_t u64;
  33. #endif /* ^__x86_64__ */
  34. typedef int8_t s8;
  35. typedef int16_t s16;
  36. typedef int32_t s32;
  37. typedef int64_t s64;
  38. #ifndef MIN
  39. # define MIN(_a,_b) ((_a) > (_b) ? (_b) : (_a))
  40. # define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
  41. #endif /* !MIN */
  42. #define SWAP16(_x) ({ \
  43. (u16)(((u16)(_x) << 8) | ((u16)(_x) >> 8)); \
  44. })
  45. #define SWAP32(_x) ({ \
  46. (u32)(((u32)(_x) << 24) | ((u32)(_x) >> 24) | \
  47. (((u32)(_x) << 8) & 0x00FF0000) | \
  48. (((u32)(_x) >> 8) & 0x0000FF00)); \
  49. })
  50. #ifdef AFL_LLVM_PASS
  51. # define AFL_R(x) (random() % (x))
  52. #else
  53. # define R(x) (random() % (x))
  54. #endif /* ^AFL_LLVM_PASS */
  55. #define STRINGIFY_INTERNAL(x) #x
  56. #define STRINGIFY(x) STRINGIFY_INTERNAL(x)
  57. #define MEM_BARRIER() \
  58. __asm__ volatile("" ::: "memory")
  59. #define likely(_x) __builtin_expect(!!(_x), 1)
  60. #define unlikely(_x) __builtin_expect(!!(_x), 0)
  61. #endif /* ! _HAVE_TYPES_H */