_Py_HashSeed basics (3.3) - Pastebin.com (original) (raw)

gpshead

Jan 13th, 2012

254

0

Never

Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

  1. $ hg diff
  2. diff --git a/Include/object.h b/Include/object.h
  3. --- a/Include/object.h
  4. +++ b/Include/object.h
  5. @@ -51,6 +51,14 @@
  6. whose size is determined when the object is allocated.
  7. */
  8. +/* Used to make bytes and unicode hashes unpredictable. */
  9. +#define Py_HASH_SEED_SUPPORTED /* TODO(gregory.p.smith): make this a configure
  10. +#ifdef Py_HASH_SEED_SUPPORTED
  11. +PyAPI_DATA(Py_hash_t) _Py_HashSeed;
  12. +#else
  13. +#define _Py_HashSeed 0
  14. +#endif
  15. /* Py_DEBUG implies Py_TRACE_REFS. */
  16. #if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
  17. #define Py_TRACE_REFS
  18. diff --git a/Objects/object.c b/Objects/object.c
  19. --- a/Objects/object.c
  20. +++ b/Objects/object.c
  21. @@ -8,6 +8,10 @@
  22. extern "C" {
  23. #endif
  24. +#ifdef Py_HASH_SEED_SUPPORTED
  25. +Py_hash_t _Py_HashSeed;
  26. +#endif
  27. #ifdef Py_REF_DEBUG
  28. Py_ssize_t _Py_RefTotal;
  29. @@ -759,7 +763,7 @@
  30. Py_uhash_t x;
  31. Py_ssize_t i;
  32. - x = (Py_uhash_t) *p << 7;
  33. + x = _Py_HashSeed ^ ((Py_uhash_t) *p << 7);
  34. for (i = 0; i < len; i++)
  35. x = (1000003U * x) ^ (Py_uhash_t) *p++;
  36. x ^= (Py_uhash_t) len;
  37. diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
  38. --- a/Objects/unicodeobject.c
  39. +++ b/Objects/unicodeobject.c
  40. @@ -11162,7 +11162,7 @@
  41. /* The hash function as a macro, gets expanded three times below. */
  42. #define HASH(P) \
  43. - x = (Py_uhash_t)*P << 7; \
  44. + x = _Py_HashSeed ^ ((Py_uhash_t)*P << 7); \
  45. while (--len >= 0) \
  46. x = (1000003*x) ^ (Py_uhash_t)*P++;
  47. diff --git a/Python/pythonrun.c b/Python/pythonrun.c
  48. --- a/Python/pythonrun.c
  49. +++ b/Python/pythonrun.c
  50. @@ -203,6 +203,12 @@
  51. initialized = 1;
  52. _Py_Finalizing = NULL;
  53. +#ifdef Py_HASH_SEED_SUPPORTED
  54. + _Py_HashSeed = 0; /* TODO(gregory.p.smith): Init from a random source. */
  55. + /* pid combined with highest resolution time possible at a minimum if
  56. + * a /dev/urandom or equivalent API is not available. */
  57. +#endif
  58. #if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
  59. /* Set up the LC_CTYPE locale, so we can obtain
  60. the locale's charset without having to switch