summaryrefslogtreecommitdiff
path: root/ext/Hash-Util
diff options
context:
space:
mode:
authorYves Orton <demerphq@gmail.com>2013-05-05 16:33:43 +0200
committerYves Orton <demerphq@gmail.com>2013-05-07 09:33:42 +0200
commit6a5b4183fb7901110d40b26a7e60b0950cc9465d (patch)
tree03097efff3b551cc07b262968b9a238f7cac6f6f /ext/Hash-Util
parent64c33badc975262c1617c4756a5a7868443e14e8 (diff)
downloadperl-6a5b4183fb7901110d40b26a7e60b0950cc9465d.tar.gz
Make it possible to disable and control hash key traversal randomization
Adds support for PERL_PERTURB_KEYS environment variable, which in turn allows one to control the level of randomization applied to keys() and friends. When PERL_PERTURB_KEYS is 0 we will not randomize key order at all. The chance that keys() changes due to an insert will be the same as in previous perls, basically only when the bucket size is changed. When PERL_PERTURB_KEYS is 1 we will randomize keys in a non repeatedable way. The chance that keys() changes due to an insert will be very high. This is the most secure and default mode. When PERL_PERTURB_KEYS is 2 we will randomize keys in a repeatedable way. Repititive runs of the same program should produce the same output every time. The chance that keys changes due to an insert will be very high. This patch also makes PERL_HASH_SEED imply a non-default PERL_PERTURB_KEYS setting. Setting PERL_HASH_SEED=0 (exactly one 0) implies PERL_PERTURB_KEYS=0 (hash key randomization disabled), settng PERL_HASH_SEED to any other value, implies PERL_PERTURB_KEYS=2 (deterministic/repeatable hash key randomization). Specifying PERL_PERTURB_KEYS explicitly to a different level overrides this behavior. Includes changes to allow one to compile out various aspects of the patch. One can compile such that PERL_PERTURB_KEYS is not respected, or can compile without hash key traversal randomization at all. Note that support for these modes is incomplete, and currently a few tests will fail. Also includes a new subroutine in Hash::Util::hash_traversal_mask() which can be used to ensure a given hash produces a predictable key order (assuming the same hash seed is in effect). This sub acts as a getter and a setter. NOTE - this patch lacks tests, but I lack tuits to get them done quickly, so I am pushing this with the hope that others can add them afterwards.
Diffstat (limited to 'ext/Hash-Util')
-rw-r--r--ext/Hash-Util/Util.xs22
-rw-r--r--ext/Hash-Util/lib/Hash/Util.pm2
2 files changed, 23 insertions, 1 deletions
diff --git a/ext/Hash-Util/Util.xs b/ext/Hash-Util/Util.xs
index c8a692f8db..33bce41d96 100644
--- a/ext/Hash-Util/Util.xs
+++ b/ext/Hash-Util/Util.xs
@@ -68,6 +68,7 @@ hash_seed()
mXPUSHs(newSVpvn((char *)PERL_HASH_SEED,PERL_HASH_SEED_BYTES));
XSRETURN(1);
+
void
hash_value(string)
SV* string
@@ -81,6 +82,27 @@ hash_value(string)
PERL_HASH(uv,pv,len);
XSRETURN_UV(uv);
+void
+hash_traversal_mask(rhv, ...)
+ SV* rhv
+ PPCODE:
+{
+#ifdef PERL_HASH_RANDOMIZE_KEYS
+ if (SvROK(rhv) && SvTYPE(SvRV(rhv))==SVt_PVHV && !SvMAGICAL(SvRV(rhv))) {
+ const HV * const hv = (const HV *) SvRV(rhv);
+ if (items>1) {
+ hv_rand_set(hv, SvUV(ST(1)));
+ }
+ if (SvOOK(hv)) {
+ XSRETURN_UV(HvRAND_get(hv));
+ } else {
+ XSRETURN_UNDEF;
+ }
+ }
+#else
+ Perl_croak(aTHX_ "Perl has not been compiled with support for randomized hash key traversal");
+#endif
+}
void
bucket_info(rhv)
diff --git a/ext/Hash-Util/lib/Hash/Util.pm b/ext/Hash-Util/lib/Hash/Util.pm
index 050f9263b0..336a28ec7e 100644
--- a/ext/Hash-Util/lib/Hash/Util.pm
+++ b/ext/Hash-Util/lib/Hash/Util.pm
@@ -32,7 +32,7 @@ our @EXPORT_OK = qw(
bucket_stats bucket_info bucket_array
lock_hash_recurse unlock_hash_recurse
);
-our $VERSION = '0.15';
+our $VERSION = '0.16';
require XSLoader;
XSLoader::load();