File: FindEncoderBugs.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (552 lines) | stat: -rw-r--r-- 20,951 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
 * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 6233345 6381699 6381702 6381705 6381706
 * @summary Encode many char sequences in many ways
 * @library /test/lib
 * @build jdk.test.lib.RandomFactory
 * @run main/timeout=1200 FindEncoderBugs
 * @author Martin Buchholz
 * @key randomness
 */

import java.util.*;
import java.util.regex.*;
import java.nio.*;
import java.nio.charset.*;
import jdk.test.lib.RandomFactory;

public class FindEncoderBugs {

    static boolean isBroken(String csn) {
        if (csn.equals("x-COMPOUND_TEXT")) return true;
        return false;
    }

    static <T extends Comparable<? super T>> List<T> sort(Collection<T> c) {
        List<T> list = new ArrayList<T>(c);
        Collections.sort(list);
        return list;
    }

    static class TooManyFailures extends RuntimeException {
        private static final long serialVersionUID = 0L;
    }

    static String string(byte[] a) {
        final StringBuilder sb = new StringBuilder();
        for (byte b : a) {
            if (sb.length() != 0) sb.append(' ');
            sb.append(String.format("%02x", b & 0xff));
        }
        return sb.toString();
    }

    static String string(char[] a) {
        final StringBuilder sb = new StringBuilder();
        for (char c : a) {
            if (sb.length() != 0) sb.append(' ');
            sb.append(String.format("\\u%04x", (int) c));
        }
        return sb.toString();
    }

    static class Reporter {
        // Some machinery to make sure only a small number of errors
        // that are "too similar" are reported.
        static class Counts extends HashMap<String, Long> {
            private static final long serialVersionUID = -1;
            long inc(String signature) {
                Long count = get(signature);
                if (count == null) count = 0L;
                put(signature, count+1);
                return count+1;
            }
        }

        final Counts failureCounts = new Counts();
        final static long maxFailures = 2;

        final static Pattern hideBytes = Pattern.compile("\"[0-9a-f ]+\"");
        final static Pattern hideChars = Pattern.compile("\\\\u[0-9a-f]{4}");

        boolean bug(String format, Object... args) {
            String signature = String.format(format, args);
            //      signature = hideBytes.matcher(signature).replaceAll("\"??\"");
            //      signature = hideChars.matcher(signature).replaceAll("\\u????");
            failed++;
            if (failureCounts.inc(signature) <= maxFailures) {
                System.out.printf(format, args);
                System.out.println();
                return true;
            }
            return false;
        }

        void summarize() {
            for (String key : sort(failureCounts.keySet()))
                System.out.printf("-----%n%s%nfailures=%d%n",
                                  key, failureCounts.get(key));
        }
    }

    static final Reporter reporter = new Reporter();

    static class Result {
        final int limit;
        final int ipos;
        final boolean direct;
        final char[] ia;
        final byte[] oa;
        final CoderResult cr;

        private static byte[] toByteArray(ByteBuffer bb) {
            byte[] bytes = new byte[bb.position()];
            for (int i = 0; i < bytes.length; i++)
                bytes[i] = bb.get(i);
            return bytes;
        }

        Result(CharBuffer ib, ByteBuffer ob, CoderResult cr) {
            ipos = ib.position();
            ia = toArray(ib);
            oa = toArray(ob);
            direct = ib.isDirect();
            limit = ob.limit();
            this.cr = cr;
        }

        static char[] toArray(CharBuffer b) {
            int pos = b.position();
            char[] a = new char[b.limit()];
            b.position(0);
            b.get(a);
            b.position(pos);
            return a;
        }

        static byte[] toArray(ByteBuffer b) {
            byte[] a = new byte[b.position()];
            b.position(0);
            b.get(a);
            return a;
        }

        static boolean eq(Result x, Result y) {
            return x == y ||
                (x != null && y != null &&
                 (Arrays.equals(x.oa, y.oa) &&
                  x.ipos == y.ipos &&
                  x.cr == y.cr));
        }

        public String toString() {
            return String.format("\"%s\"[%d/%d] => %s \"%s\"[%d/%d]%s",
                                 string(ia), ipos, ia.length,
                                 cr, string(oa), oa.length, limit,
                                 (direct ? " (direct)" : ""));
        }
    }

    static class CharsetTester {
        private final Charset cs;
        private final boolean hasBom;
        private static final int maxFailures = 5;
        private int failures = 0;
        // private static final long maxCharsetFailures = Long.MAX_VALUE;
        private static final long maxCharsetFailures = 10000L;
        private final long failed0 = failed;

        // legend: r=regular d=direct In=Input Ou=Output
        static final int maxBufSize = 40;
        static final CharBuffer[] rInBuffers = new CharBuffer[maxBufSize];
        static final CharBuffer[] dInBuffers = new CharBuffer[maxBufSize];

        static final ByteBuffer[] rOuBuffers = new ByteBuffer[maxBufSize];
        static final ByteBuffer[] dOuBuffers = new ByteBuffer[maxBufSize];
        static {
            for (int i = 0; i < maxBufSize; i++) {
                rInBuffers[i] = CharBuffer.allocate(i);
                dInBuffers[i] = ByteBuffer.allocateDirect(i*2).asCharBuffer();
                rOuBuffers[i] = ByteBuffer.allocate(i);
                dOuBuffers[i] = ByteBuffer.allocateDirect(i);
            }
        }

        CharsetTester(Charset cs) {
            this.cs = cs;
            this.hasBom =
                cs.name().matches(".*BOM.*") ||
                cs.name().equals("UTF-16");
        }

        static boolean bug(String format, Object... args) {
            return reporter.bug(format, args);
        }

        static boolean hasBom(byte[] a) {
            switch (a.length) {
            case 2: case 4:
                int sum = 0;
                for (byte x : a)
                    sum += x;
                return sum == (byte) 0xfe + (byte) 0xff;
            default: return false;
            }
        }

        void testSurrogates() {
            int failures = 0;
            for (int i = 0; i < 10; i++) {
                Result r = test(new char[] { randomHighSurrogate() });
                if (r == null) break;
                if (! (r.cr.isUnderflow() &&
                       r.ipos == 0))
                    bug("Lone high surrogate not UNDERFLOW: %s %s",
                        cs, r);
            }
            for (int i = 0; i < 10; i++) {
                Result r = test(new char[] { randomLowSurrogate() });
                if (r == null) break;
                if (! (r.cr.isMalformed() && r.cr.length() == 1))
                    bug("Lone low surrogate not MALFORMED[1]: %s %s",
                        cs, r);
            }
            char[] chars = new char[2];
            for (int i = 0; i < 10; i++) {
                chars[0] = randomLowSurrogate(); // Always illegal
                chars[1] = randomChar();
                Result r = test(chars);
                if (r == null) break;
                if (! (r.cr.isMalformed() &&
                       r.cr.length() == 1 &&
                       (r.ipos == 0 || (hasBom && hasBom(r.oa))))) {
                    if (failures++ > 5) return;
                    bug("Unpaired low surrogate not MALFORMED[1]: %s %s",
                        cs, r);
                }
            }
            for (int i = 0; i < 10; i++) {
                chars[0] = randomHighSurrogate();
                do {
                    chars[1] = randomChar();
                } while (Character.isLowSurrogate(chars[1]));
                Result r = test(chars);
                if (r == null) break;
                if (! (r.cr.isMalformed() &&
                       r.cr.length() == 1 &&
                       (r.ipos == 0 || (hasBom && hasBom(r.oa))))) {
                    if (failures++ > 5) return;
                    bug("Unpaired high surrogate not MALFORMED[1]: %s %s",
                        cs, r);
                }
            }
            for (int i = 0; i < 1000; i++) {
                chars[0] = randomHighSurrogate();
                chars[1] = randomLowSurrogate();
                Result r = test(chars);
                if (r == null) break;
                if (! ((r.cr.isUnmappable() &&
                        r.cr.length() == 2 &&
                        r.oa.length == 0)
                       ||
                       (r.cr.isUnderflow() &&
                        r.oa.length > 0 &&
                        r.ipos == 2))) {
                    if (failures++ > 5) return;
                    bug("Legal supplementary character bug: %s %s",
                        cs, r);
                }
            }
        }

//              if (! (r.cr.isMalformed() &&
//                     r.cr.length() == 1 &&
//                     (rob.position() == 0 || hasBom(rob)))) {
//                  if (failures++ > 5) return;
//                  bug("Unpaired surrogate not malformed: %s %s",
//                               cs, r);
//              }
//          }

//                  dib.clear(); dib.put(chars); dib.flip();
//                  rib.position(0);
//                  rob.clear(); rob.limit(lim);
//                  for (CharBuffer ib : new CharBuffer[] { rib, dib }) {
//                      Result r = recode(ib, rob);
//                      if (! (r.cr.isMalformed() &&
//                             r.cr.length() == 1 &&
//                             (rob.position() == 0 || hasBom(rob)))) {
//                          if (failures++ > 5) return;
//                          bug("Unpaired surrogate not malformed: %s %s",
//                                       cs, r);
//                      }
//                  }
//                  //}
//              for (int i = 0; i < 10000; i++) {
//                  chars[0] = randomHighSurrogate();
//                  chars[1] = randomLowSurrogate();
//                  dib.clear(); dib.put(chars); dib.flip();
//                  rib.position(0);
//                  rob.clear(); rob.limit(lim);
//                  for (CharBuffer ib : new CharBuffer[] { rib, dib }) {
//                      Result r = recode(ib, rob);
//                      if (! ((r.cr.isUnmappable() &&
//                              r.cr.length() == 2 &&
//                              rob.position() == 0)
//                             ||
//                             (r.cr.isUnderflow() &&
//                              rob.position() > 0 &&
//                              ib.position() == 2))) {
//                          if (failures++ > 5) return;
//                          bug("Legal supplementary character bug: %s %s",
//                                       cs, r);
//                      }
//                  }
//              }
//          }
//      }

        Result recode(CharBuffer ib, ByteBuffer ob) {
            try {
                byte canary = 22;
                ib.clear();     // Prepare to read
                ob.clear();     // Prepare to write
                for (int i = 0; i < ob.limit(); i++)
                    ob.put(i, canary);
                CharsetEncoder coder = cs.newEncoder();
                CoderResult cr = coder.encode(ib, ob, false);
                equal(ib.limit(), ib.capacity());
                equal(ob.limit(), ob.capacity());
                Result r = new Result(ib, ob, cr);
                if (cr.isError())
                    check(cr.length() > 0);
                if (cr.isOverflow() && ob.remaining() > 10)
                    bug("OVERFLOW, but there's lots of room: %s %s",
                        cs, r);
//              if (cr.isOverflow() && ib.remaining() == 0 && ! hasBom)
//                  bug("OVERFLOW, yet remaining() == 0: %s %s",
//                      cs, r);
                if (cr.isError() && ib.remaining() < cr.length())
                    bug("remaining() < CoderResult.length(): %s %s",
                        cs, r);
//              if (ib.position() == 0
//                  && ob.position() > 0
//                  && ! hasBom(r.oa))
//                  bug("output only if input consumed: %s %s",
//                       cs, r);
                CoderResult cr2 = coder.encode(ib, ob, false);
                if (ib.position() != r.ipos ||
                    ob.position() != r.oa.length ||
                    cr != cr2)
                    bug("Coding operation not idempotent: %s%n    %s%n    %s",
                        cs, r, new Result(ib, ob, cr2));
                if (ob.position() < ob.limit() &&
                    ob.get(ob.position()) != canary)
                    bug("Buffer overrun: %s %s %s",
                        cs, r, ob.get(ob.position()));
                return r;
            } catch (Throwable t) {
                if (bug("Unexpected exception: %s %s %s",
                        cs, t.getClass().getSimpleName(),
                        new Result(ib, ob, null)))
                    t.printStackTrace();
                return null;
            }
        }

        Result recode2(char[] ia, int n) {
            int len = ia.length;
            CharBuffer rib = CharBuffer.wrap(ia);
            CharBuffer dib = dInBuffers[len];
            dib.clear(); dib.put(ia); dib.clear();
            ByteBuffer rob = rOuBuffers[n];
            ByteBuffer dob = dOuBuffers[n];
            equal(rob.limit(), n);
            equal(dob.limit(), n);
            check(dib.isDirect());
            check(dob.isDirect());
            Result r1 = recode(rib, rob);
            Result r2 = recode(dib, dob);
            if (r1 != null && r2 != null && ! Result.eq(r1, r2))
                bug("Results differ for direct buffers: %s%n    %s%n    %s",
                    cs, r1, r2);
            return r1;
        }

        Result test(char[] ia) {
            if (failed - failed0 >= maxCharsetFailures)
                throw new TooManyFailures();

            Result roomy = recode2(ia, maxBufSize - 1);
            if (roomy == null) return roomy;
            int olen = roomy.oa.length;
            if (olen > 0) {
                if (roomy.ipos == roomy.ia.length) {
                    Result perfectFit = recode2(ia, olen);
                    if (! Result.eq(roomy, perfectFit))
                        bug("Results differ: %s%n    %s%n    %s",
                            cs, roomy, perfectFit);
                }
                for (int i = 0; i < olen; i++) {
                    Result claustrophobic = recode2(ia, i);
                    if (claustrophobic == null) return roomy;
                    if (roomy.cr.isUnderflow() &&
                        ! claustrophobic.cr.isOverflow())
                        bug("Expected OVERFLOW: %s%n    %s%n    %s",
                            cs, roomy, claustrophobic);
                }
            }
            return roomy;
        }

        void testExhaustively(char[] prefix, int n) {
            int len = prefix.length;
            char[] ia = Arrays.copyOf(prefix, len + 1);
            for (int i = 0; i < 0x10000; i++) {
                ia[len] = (char) i;
                if (n == 1)
                    test(ia);
                else
                    testExhaustively(ia, n - 1);
            }
        }

        void testRandomly(char[] prefix, int n) {
            int len = prefix.length;
            char[] ia = Arrays.copyOf(prefix, len + n);
            for (int i = 0; i < 10000; i++) {
                for (int j = 0; j < n; j++)
                    ia[len + j] = randomChar();
                test(ia);
            }
        }

        void testISO88591InvalidChar() {
            // Several architectures implement the ISO-8859-1 encoder as an
            // intrinsic where the vectorised assembly has separate cases
            // for different input sizes, so exhaustively test all sizes
            // from 0 to maxBufSize to ensure we get coverage

            for (int i = 0; i < CharsetTester.maxBufSize; i++) {
                char[] ia = new char[i];
                for (int j = 0; j < i; j++)
                    ia[j] = randomChar();

                test(ia);

                // Test break on unrepresentable character
                for (int j = 0; j < i; j++) {
                    char[] iaInvalid = ia.clone();
                    iaInvalid[j] = (char)(randomChar() | 0x100);
                    test(iaInvalid);
                }
            }
        }

        void testPrefix(char[] prefix) {
            if (prefix.length > 0)
                System.out.printf("Testing prefix %s%n", string(prefix));

            test(prefix);

            testExhaustively(prefix, 1);
            // Can you spare a year of CPU time?
            //testExhaustively(prefix, 2);

            testRandomly(prefix, 2);
            testRandomly(prefix, 3);
        }
    }

    private final static Random rnd = RandomFactory.getRandom();
    private static char randomChar() {
        return (char) rnd.nextInt(Character.MAX_VALUE);
    }
    private static char randomHighSurrogate() {
        return (char) (Character.MIN_HIGH_SURROGATE + rnd.nextInt(1024));
    }
    private static char randomLowSurrogate() {
        return (char) (Character.MIN_LOW_SURROGATE + rnd.nextInt(1024));
    }

    private static void testCharset(Charset cs) throws Throwable {
        if (! cs.canEncode())
            return;

        final String csn = cs.name();

        if (isBroken(csn)) {
            System.out.printf("Skipping possibly broken charset %s%n", csn);
            return;
        }
        System.out.println(csn);

        CharsetTester tester = new CharsetTester(cs);

        tester.testSurrogates();

        tester.testPrefix(new char[] {});

        if (csn.equals("x-ISCII91")) {
            System.out.println("More ISCII testing...");
            new CharsetTester(cs).testPrefix(new char[]{'\u094d'}); // Halant
            new CharsetTester(cs).testPrefix(new char[]{'\u093c'}); // Nukta
        } else if (csn.equals("ISO-8859-1")) {
            System.out.println("More ISO-8859-1 testing...");
            new CharsetTester(cs).testISO88591InvalidChar();
        }
    }

    private static void realMain(String[] args) {
        for (Charset cs : sort(Charset.availableCharsets().values())) {
            try {
                testCharset(cs);
            } catch (TooManyFailures e) {
                System.out.printf("Too many failures for %s%n", cs);
            } catch (Throwable t) {
                unexpected(t);
            }
        }
        reporter.summarize();
    }

    //--------------------- Infrastructure ---------------------------
    static volatile long passed = 0, failed = 0;
    static void pass() {passed++;}
    static void fail() {failed++; Thread.dumpStack();}
    static void fail(String format, Object... args) {
        System.out.println(String.format(format, args)); failed++;}
    static void fail(String msg) {System.out.println(msg); fail();}
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
    static void check(boolean cond) {if (cond) pass(); else fail();}
    static void equal(Object x, Object y) {
        if (x == null ? y == null : x.equals(y)) pass();
        else fail(x + " not equal to " + y);}
    public static void main(String[] args) throws Throwable {
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
        if (failed > 0) throw new AssertionError("Some tests failed");}
}