00db10
commit 9795a1801eb1a4f3ae6346e32666d3d05f006115
00db10
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
00db10
Date:   Sun Jun 30 20:45:05 2013 +0530
00db10
00db10
    Fall back to non-cached sequence traversal and comparison
00db10
    
00db10
    strcoll currently falls back to alloca if malloc fails, resulting in a
00db10
    possible stack overflow.  This patch implements sequence traversal and
00db10
    comparison without caching indeces and rules.
00db10
00db10
diff --git glibc-2.17-c758a686/string/strcoll_l.c glibc-2.17-c758a686/string/strcoll_l.c
00db10
index 1bb9e23..1be6874 100644
00db10
--- glibc-2.17-c758a686/string/strcoll_l.c
00db10
+++ glibc-2.17-c758a686/string/strcoll_l.c
00db10
@@ -55,6 +55,12 @@ typedef struct
00db10
   const USTRING_TYPE *us;	/* The string.  */
00db10
   int32_t *idxarr;		/* Array to cache weight indeces.  */
00db10
   unsigned char *rulearr;	/* Array to cache rules.  */
00db10
+  unsigned char rule;		/* Saved rule for the first sequence.  */
00db10
+  int32_t idx;			/* Index to weight of the current sequence.  */
00db10
+  int32_t save_idx;		/* Save looked up index of a forward
00db10
+				   sequence after the last backward
00db10
+				   sequence.  */
00db10
+  const USTRING_TYPE *back_us;	/* Beginning of the backward sequence.  */
00db10
 } coll_seq;
00db10
 
00db10
 /* Get next sequence.  The weight indeces are cached, so we don't need to
00db10
@@ -227,7 +233,191 @@ get_next_seq (coll_seq *seq, int nrules, const unsigned char *rulesets,
00db10
   seq->us = us;
00db10
 }
00db10
 
00db10
-/* Compare two sequences.  */
00db10
+/* Get next sequence.  Traverse the string as required.  This function does not
00db10
+   set or use any index or rule cache.  */
00db10
+static void
00db10
+get_next_seq_nocache (coll_seq *seq, int nrules, const unsigned char *rulesets,
00db10
+		      const USTRING_TYPE *weights, const int32_t *table,
00db10
+		      const USTRING_TYPE *extra, const int32_t *indirect,
00db10
+		      int pass)
00db10
+{
00db10
+#include WEIGHT_H
00db10
+  int val = seq->val = 0;
00db10
+  int len = seq->len;
00db10
+  size_t backw_stop = seq->backw_stop;
00db10
+  size_t backw = seq->backw;
00db10
+  size_t idxcnt = seq->idxcnt;
00db10
+  size_t idxmax = seq->idxmax;
00db10
+  int32_t idx = seq->idx;
00db10
+  const USTRING_TYPE *us = seq->us;
00db10
+
00db10
+  while (len == 0)
00db10
+    {
00db10
+      ++val;
00db10
+      if (backw_stop != ~0ul)
00db10
+	{
00db10
+	  /* The is something pushed.  */
00db10
+	  if (backw == backw_stop)
00db10
+	    {
00db10
+	      /* The last pushed character was handled.  Continue
00db10
+		 with forward characters.  */
00db10
+	      if (idxcnt < idxmax)
00db10
+		{
00db10
+		  idx = seq->save_idx;
00db10
+		  backw_stop = ~0ul;
00db10
+		}
00db10
+	      else
00db10
+		{
00db10
+		  /* Nothing anymore.  The backward sequence ended with
00db10
+		     the last sequence in the string.  Note that len is
00db10
+		     still zero.  */
00db10
+		  idx = 0;
00db10
+		  break;
00db10
+	        }
00db10
+	    }
00db10
+	  else
00db10
+	    {
00db10
+	      /* XXX Traverse BACKW sequences from the beginning of
00db10
+		 BACKW_STOP to get the next sequence.  Is ther a quicker way
00db10
+	         to do this?  */
00db10
+	      int i = backw_stop;
00db10
+	      us = seq->back_us;
00db10
+	      while (i < backw)
00db10
+		{
00db10
+		  int32_t tmp = findidx (&us, -1);
00db10
+		  idx = tmp & 0xffffff;
00db10
+		  i++;
00db10
+		}
00db10
+	      --backw;
00db10
+	      us = seq->us;
00db10
+	    }
00db10
+	}
00db10
+      else
00db10
+	{
00db10
+	  backw_stop = idxmax;
00db10
+	  int32_t prev_idx = idx;
00db10
+
00db10
+	  while (*us != L('\0'))
00db10
+	    {
00db10
+	      int32_t tmp = findidx (&us, -1);
00db10
+	      unsigned char rule = tmp >> 24;
00db10
+	      prev_idx = idx;
00db10
+	      idx = tmp & 0xffffff;
00db10
+	      idxcnt = idxmax++;
00db10
+
00db10
+	      /* Save the rule for the first sequence.  */
00db10
+	      if (__glibc_unlikely (idxcnt == 0))
00db10
+	        seq->rule = rule;
00db10
+
00db10
+	      if ((rulesets[rule * nrules + pass]
00db10
+		   & sort_backward) == 0)
00db10
+		/* No more backward characters to push.  */
00db10
+		break;
00db10
+	      ++idxcnt;
00db10
+	    }
00db10
+
00db10
+	  if (backw_stop >= idxcnt)
00db10
+	    {
00db10
+	      /* No sequence at all or just one.  */
00db10
+	      if (idxcnt == idxmax || backw_stop > idxcnt)
00db10
+		/* Note that len is still zero.  */
00db10
+		break;
00db10
+
00db10
+	      backw_stop = ~0ul;
00db10
+	    }
00db10
+	  else
00db10
+	    {
00db10
+	      /* We pushed backward sequences.  If the stream ended with the
00db10
+		 backward sequence, then we process the last sequence we
00db10
+		 found.  Otherwise we process the sequence before the last
00db10
+		 one since the last one was a forward sequence.  */
00db10
+	      seq->back_us = seq->us;
00db10
+	      seq->us = us;
00db10
+	      backw = idxcnt;
00db10
+	      if (idxmax > idxcnt)
00db10
+		{
00db10
+		  backw--;
00db10
+		  seq->save_idx = idx;
00db10
+		  idx = prev_idx;
00db10
+		}
00db10
+	      if (backw > backw_stop)
00db10
+		backw--;
00db10
+	    }
00db10
+	}
00db10
+
00db10
+      len = weights[idx++];
00db10
+      /* Skip over indeces of previous levels.  */
00db10
+      for (int i = 0; i < pass; i++)
00db10
+	{
00db10
+	  idx += len;
00db10
+	  len = weights[idx];
00db10
+	  idx++;
00db10
+	}
00db10
+    }
00db10
+
00db10
+  /* Update the structure.  */
00db10
+  seq->val = val;
00db10
+  seq->len = len;
00db10
+  seq->backw_stop = backw_stop;
00db10
+  seq->backw = backw;
00db10
+  seq->idxcnt = idxcnt;
00db10
+  seq->idxmax = idxmax;
00db10
+  seq->us = us;
00db10
+  seq->idx = idx;
00db10
+}
00db10
+
00db10
+/* Compare two sequences.  This version does not use the index and rules
00db10
+   cache.  */
00db10
+static int
00db10
+do_compare_nocache (coll_seq *seq1, coll_seq *seq2, int position,
00db10
+		    const USTRING_TYPE *weights)
00db10
+{
00db10
+  int seq1len = seq1->len;
00db10
+  int seq2len = seq2->len;
00db10
+  int val1 = seq1->val;
00db10
+  int val2 = seq2->val;
00db10
+  int idx1 = seq1->idx;
00db10
+  int idx2 = seq2->idx;
00db10
+  int result = 0;
00db10
+
00db10
+  /* Test for position if necessary.  */
00db10
+  if (position && val1 != val2)
00db10
+    {
00db10
+      result = val1 - val2;
00db10
+      goto out;
00db10
+    }
00db10
+
00db10
+  /* Compare the two sequences.  */
00db10
+  do
00db10
+    {
00db10
+      if (weights[idx1] != weights[idx2])
00db10
+	{
00db10
+	  /* The sequences differ.  */
00db10
+	  result = weights[idx1] - weights[idx2];
00db10
+	  goto out;
00db10
+	}
00db10
+
00db10
+      /* Increment the offsets.  */
00db10
+      ++idx1;
00db10
+      ++idx2;
00db10
+
00db10
+      --seq1len;
00db10
+      --seq2len;
00db10
+    }
00db10
+  while (seq1len > 0 && seq2len > 0);
00db10
+
00db10
+  if (position && seq1len != seq2len)
00db10
+    result = seq1len - seq2len;
00db10
+
00db10
+out:
00db10
+  seq1->len = seq1len;
00db10
+  seq2->len = seq2len;
00db10
+  seq1->idx = idx1;
00db10
+  seq2->idx = idx2;
00db10
+  return result;
00db10
+}
00db10
+
00db10
+/* Compare two sequences using the index cache.  */
00db10
 static int
00db10
 do_compare (coll_seq *seq1, coll_seq *seq2, int position,
00db10
 	    const USTRING_TYPE *weights)
00db10
@@ -334,57 +524,62 @@ STRCOLL (const STRING_TYPE *s1, const STRING_TYPE *s2, __locale_t l)
00db10
   memset (&seq1, 0, sizeof (seq1));
00db10
   seq2 = seq1;
00db10
 
00db10
-  /* We need the elements of the strings as unsigned values since they
00db10
-     are used as indeces.  */
00db10
-  seq1.us = (const USTRING_TYPE *) s1;
00db10
-  seq2.us = (const USTRING_TYPE *) s2;
00db10
-
00db10
   if (! __libc_use_alloca ((s1len + s2len) * (sizeof (int32_t) + 1)))
00db10
     {
00db10
       seq1.idxarr = (int32_t *) malloc ((s1len + s2len) * (sizeof (int32_t) + 1));
00db10
-      seq2.idxarr = &seq1.idxarr[s1len];
00db10
-      seq1.rulearr = (unsigned char *) &seq2.idxarr[s2len];
00db10
-      seq2.rulearr = &seq1.rulearr[s1len];
00db10
-
00db10
-      if (seq1.idxarr == NULL)
00db10
-	/* No memory.  Well, go with the stack then.
00db10
-
00db10
-	   XXX Once this implementation is stable we will handle this
00db10
-	   differently.  Instead of precomputing the indeces we will
00db10
-	   do this in time.  This means, though, that this happens for
00db10
-	   every pass again.  */
00db10
-	goto try_stack;
00db10
-      use_malloc = true;
00db10
+
00db10
+      /* If we failed to allocate memory, we leave everything as NULL so that
00db10
+	 we use the nocache version of traversal and comparison functions.  */
00db10
+      if (seq1.idxarr != NULL)
00db10
+	{
00db10
+	  seq2.idxarr = &seq1.idxarr[s1len];
00db10
+	  seq1.rulearr = (unsigned char *) &seq2.idxarr[s2len];
00db10
+	  seq2.rulearr = &seq1.rulearr[s1len];
00db10
+	  use_malloc = true;
00db10
+	}
00db10
     }
00db10
   else
00db10
     {
00db10
-    try_stack:
00db10
       seq1.idxarr = (int32_t *) alloca (s1len * sizeof (int32_t));
00db10
       seq2.idxarr = (int32_t *) alloca (s2len * sizeof (int32_t));
00db10
       seq1.rulearr = (unsigned char *) alloca (s1len);
00db10
       seq2.rulearr = (unsigned char *) alloca (s2len);
00db10
     }
00db10
 
00db10
-  seq1.rulearr[0] = 0;
00db10
+  int rule = 0;
00db10
 
00db10
   /* Cache values in the first pass and if needed, use them in subsequent
00db10
      passes.  */
00db10
   for (int pass = 0; pass < nrules; ++pass)
00db10
     {
00db10
       seq1.idxcnt = 0;
00db10
+      seq1.idx = 0;
00db10
+      seq2.idx = 0;
00db10
       seq1.backw_stop = ~0ul;
00db10
       seq1.backw = ~0ul;
00db10
       seq2.idxcnt = 0;
00db10
       seq2.backw_stop = ~0ul;
00db10
       seq2.backw = ~0ul;
00db10
 
00db10
+      /* We need the elements of the strings as unsigned values since they
00db10
+	 are used as indeces.  */
00db10
+      seq1.us = (const USTRING_TYPE *) s1;
00db10
+      seq2.us = (const USTRING_TYPE *) s2;
00db10
+
00db10
       /* We assume that if a rule has defined `position' in one section
00db10
 	 this is true for all of them.  */
00db10
-      int position = rulesets[seq1.rulearr[0] * nrules + pass] & sort_position;
00db10
+      int position = rulesets[rule * nrules + pass] & sort_position;
00db10
 
00db10
       while (1)
00db10
 	{
00db10
-	  if (pass == 0)
00db10
+	  if (__glibc_unlikely (seq1.idxarr == NULL))
00db10
+	    {
00db10
+	      get_next_seq_nocache (&seq1, nrules, rulesets, weights, table,
00db10
+				    extra, indirect, pass);
00db10
+	      get_next_seq_nocache (&seq2, nrules, rulesets, weights, table,
00db10
+				    extra, indirect, pass);
00db10
+	    }
00db10
+	  else if (pass == 0)
00db10
 	    {
00db10
 	      get_next_seq (&seq1, nrules, rulesets, weights, table, extra,
00db10
 			    indirect);
00db10
@@ -411,10 +606,18 @@ STRCOLL (const STRING_TYPE *s1, const STRING_TYPE *s2, __locale_t l)
00db10
 	      goto free_and_return;
00db10
 	    }
00db10
 
00db10
-	  result = do_compare (&seq1, &seq2, position, weights);
00db10
+	  if (__glibc_unlikely (seq1.idxarr == NULL))
00db10
+	    result = do_compare_nocache (&seq1, &seq2, position, weights);
00db10
+	  else
00db10
+	    result = do_compare (&seq1, &seq2, position, weights);
00db10
 	  if (result != 0)
00db10
 	    goto free_and_return;
00db10
 	}
00db10
+
00db10
+      if (__glibc_likely (seq1.rulearr != NULL))
00db10
+	rule = seq1.rulearr[0];
00db10
+      else
00db10
+	rule = seq1.rule;
00db10
     }
00db10
 
00db10
   /* Free the memory if needed.  */