components/perl/perl516/patches/rehash-5.16.2.patch
author Craig Mohrman <craig.mohrman@oracle.com>
Fri, 22 Mar 2013 12:37:36 -0700
branchs11-update
changeset 2528 788328aeec2a
permissions -rw-r--r--
PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default 15818668 SUNBT7201230 update perl to 5.16 16096782 Userland Perl should use mediated links 15941379 perl512 "gmake test" failures 15820486 problem in UTILITY/PERL 15880426 problem in UTILITY/PERL 16417744 problem in UTILITY/PERL 16383261 problem in UTILITY/PERL 16427786 problem in UTILITY/PERL 16486887 perl 5.16 needs to build x86 64-bit properly
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2528
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
     1
From f1220d61455253b170e81427c9d0357831ca0fac Mon Sep 17 00:00:00 2001
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
     2
From: Yves Orton <[email protected]>
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
     3
Date: Tue, 12 Feb 2013 10:53:05 +0100
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
     4
Subject: [PATCH] Prevent premature hsplit() calls, and only trigger REHASH
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
     5
 after hsplit()
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
     6
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
     7
Triggering a hsplit due to long chain length allows an attacker
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
     8
to create a carefully chosen set of keys which can cause the hash
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
     9
to use 2 * (2**32) * sizeof(void *) bytes ram. AKA a DOS via memory
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    10
exhaustion. Doing so also takes non trivial time.
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    11
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    12
Eliminating this check, and only inspecting chain length after a
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    13
normal hsplit() (triggered when keys>buckets) prevents the attack
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    14
entirely, and makes such attacks relatively benign.
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    15
---
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    16
 ext/Hash-Util-FieldHash/t/10_hash.t | 18 ++++++++++++++++--
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    17
 hv.c                                | 35 ++++++++---------------------------
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    18
 t/op/hash.t                         | 20 +++++++++++++++++---
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    19
 3 files changed, 41 insertions(+), 32 deletions(-)
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    20
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    21
diff --git a/ext/Hash-Util-FieldHash/t/10_hash.t b/ext/Hash-Util-FieldHash/t/10_hash.t
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    22
index 2cfb4e8..d58f053 100644
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    23
--- a/ext/Hash-Util-FieldHash/t/10_hash.t
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    24
+++ b/ext/Hash-Util-FieldHash/t/10_hash.t
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    25
@@ -38,15 +38,29 @@ use constant START     => "a";
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    26
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    27
 # some initial hash data
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    28
 fieldhash my %h2;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    29
-%h2 = map {$_ => 1} 'a'..'cc';
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    30
+my $counter= "a";
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    31
+$h2{$counter++}++ while $counter ne 'cd';
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    32
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    33
 ok (!Internals::HvREHASH(%h2), 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    34
     "starting with pre-populated non-pathological hash (rehash flag if off)");
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    35
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    36
 my @keys = get_keys(\%h2);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    37
+my $buckets= buckets(\%h2);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    38
 $h2{$_}++ for @keys;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    39
+$h2{$counter++}++ while buckets(\%h2) == $buckets; # force a split
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    40
 ok (Internals::HvREHASH(%h2), 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    41
-    scalar(@keys) . " colliding into the same bucket keys are triggering rehash");
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    42
+    scalar(@keys) . " colliding into the same bucket keys are triggering rehash after split");
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    43
+
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    44
+# returns the number of buckets in a hash
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    45
+sub buckets {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    46
+    my $hr = shift;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    47
+    my $keys_buckets= scalar(%$hr);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    48
+    if ($keys_buckets=~m!/([0-9]+)\z!) {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    49
+        return 0+$1;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    50
+    } else {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    51
+        return 8;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    52
+    }
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    53
+}
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    54
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    55
 sub get_keys {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    56
     my $hr = shift;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    57
diff --git a/hv.c b/hv.c
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    58
index 6b66251..a031703 100644
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    59
--- a/hv.c
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    60
+++ b/hv.c
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    61
@@ -35,7 +35,8 @@ holds the key and hash value.
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    62
 #define PERL_HASH_INTERNAL_ACCESS
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    63
 #include "perl.h"
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    64
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    65
-#define HV_MAX_LENGTH_BEFORE_SPLIT 14
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    66
+#define HV_MAX_LENGTH_BEFORE_REHASH 14
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    67
+#define SHOULD_DO_HSPLIT(xhv) ((xhv)->xhv_keys > (xhv)->xhv_max) /* HvTOTALKEYS(hv) > HvMAX(hv) */
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    68
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    69
 static const char S_strtab_error[]
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    70
     = "Cannot modify shared string table in hv_%s";
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    71
@@ -798,29 +799,9 @@ Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    72
     if (masked_flags & HVhek_ENABLEHVKFLAGS)
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    73
 	HvHASKFLAGS_on(hv);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    74
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    75
-    {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    76
-	const HE *counter = HeNEXT(entry);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    77
-
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    78
-	xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    79
-	if (!counter) {				/* initial entry? */
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    80
-	} else if (xhv->xhv_keys > xhv->xhv_max) {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    81
-		/* Use only the old HvUSEDKEYS(hv) > HvMAX(hv) condition to limit
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    82
-		   bucket splits on a rehashed hash, as we're not going to
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    83
-		   split it again, and if someone is lucky (evil) enough to
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    84
-		   get all the keys in one list they could exhaust our memory
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    85
-		   as we repeatedly double the number of buckets on every
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    86
-		   entry. Linear search feels a less worse thing to do.  */
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    87
-	    hsplit(hv);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    88
-	} else if(!HvREHASH(hv)) {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    89
-	    U32 n_links = 1;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    90
-
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    91
-	    while ((counter = HeNEXT(counter)))
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    92
-		n_links++;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    93
-
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    94
-	    if (n_links > HV_MAX_LENGTH_BEFORE_SPLIT) {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    95
-		hsplit(hv);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    96
-	    }
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    97
-	}
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    98
+    xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
    99
+    if ( SHOULD_DO_HSPLIT(xhv) ) {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   100
+        hsplit(hv);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   101
     }
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   102
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   103
     if (return_svp) {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   104
@@ -1197,7 +1178,7 @@ S_hsplit(pTHX_ HV *hv)
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   105
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   106
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   107
     /* Pick your policy for "hashing isn't working" here:  */
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   108
-    if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked?  */
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   109
+    if (longest_chain <= HV_MAX_LENGTH_BEFORE_REHASH /* split worked?  */
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   110
 	|| HvREHASH(hv)) {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   111
 	return;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   112
     }
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   113
@@ -2782,8 +2763,8 @@ S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   114
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   115
 	xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   116
 	if (!next) {			/* initial entry? */
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   117
-	} else if (xhv->xhv_keys > xhv->xhv_max /* HvUSEDKEYS(hv) > HvMAX(hv) */) {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   118
-		hsplit(PL_strtab);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   119
+	} else if ( SHOULD_DO_HSPLIT(xhv) ) {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   120
+            hsplit(PL_strtab);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   121
 	}
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   122
     }
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   123
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   124
diff --git a/t/op/hash.t b/t/op/hash.t
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   125
index ef757a3..97eb81b 100644
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   126
--- a/t/op/hash.t
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   127
+++ b/t/op/hash.t
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   128
@@ -39,22 +39,36 @@ use constant THRESHOLD => 14;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   129
 use constant START     => "a";
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   130
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   131
 # some initial hash data
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   132
-my %h2 = map {$_ => 1} 'a'..'cc';
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   133
+my %h2;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   134
+my $counter= "a";
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   135
+$h2{$counter++}++ while $counter ne 'cd';
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   136
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   137
 ok (!Internals::HvREHASH(%h2), 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   138
     "starting with pre-populated non-pathological hash (rehash flag if off)");
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   139
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   140
 my @keys = get_keys(\%h2);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   141
+my $buckets= buckets(\%h2);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   142
 $h2{$_}++ for @keys;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   143
+$h2{$counter++}++ while buckets(\%h2) == $buckets; # force a split
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   144
 ok (Internals::HvREHASH(%h2), 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   145
-    scalar(@keys) . " colliding into the same bucket keys are triggering rehash");
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   146
+    scalar(@keys) . " colliding into the same bucket keys are triggering rehash after split");
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   147
+
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   148
+# returns the number of buckets in a hash
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   149
+sub buckets {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   150
+    my $hr = shift;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   151
+    my $keys_buckets= scalar(%$hr);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   152
+    if ($keys_buckets=~m!/([0-9]+)\z!) {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   153
+        return 0+$1;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   154
+    } else {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   155
+        return 8;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   156
+    }
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   157
+}
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   158
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   159
 sub get_keys {
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   160
     my $hr = shift;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   161
 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   162
     # the minimum of bits required to mount the attack on a hash
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   163
     my $min_bits = log(THRESHOLD)/log(2);
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   164
-
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   165
     # if the hash has already been populated with a significant amount
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   166
     # of entries the number of mask bits can be higher
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   167
     my $keys = scalar keys %$hr;
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   168
-- 
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   169
1.8.1.3
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   170
788328aeec2a PSARC/2012/379 Add Perl 5.16 64-bit and make it the system default
Craig Mohrman <craig.mohrman@oracle.com>
parents:
diff changeset
   171