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