test/jdk/java/security/Provider/GetServiceRace.java
author phh
Wed, 25 Sep 2019 08:29:03 -0700
changeset 58337 270abce77942
permissions -rw-r--r--
8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class Summary: Synchronize access to legacyMap in Provider.getService. Reviewed-by: valeriep Contributed-by: Tianmin Shi <tianshi@amazon.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58337
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
     1
/*
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
     4
 *
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
     7
 * published by the Free Software Foundation.
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
     8
 *
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    13
 * accompanied this code).
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    14
 *
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    18
 *
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    21
 * questions.
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    22
 */
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    23
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    24
/*
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    25
 * @test
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    26
 * @bug 8231387
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    27
 * @library ../testlibrary
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    28
 * @summary make sure getService() avoids a race
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    29
 * @author Tianmin Shi
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    30
 */
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    31
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    32
import java.security.Provider;
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    33
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    34
public class GetServiceRace {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    35
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    36
    private static final Provider testProvider;
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    37
    static {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    38
        testProvider = new Provider("MyProvider", 1.0, "test") {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    39
        };
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    40
        testProvider.put("CertificateFactory.Fixed", "MyCertificateFactory");
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    41
    }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    42
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    43
    private static final int NUMBER_OF_RETRIEVERS = 3;
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    44
    private static final int TEST_TIME_MS = 1000;
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    45
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    46
    public static boolean testFailed = false;
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    47
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    48
    public static void main(String[] args) throws Exception {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    49
        Updater updater = new Updater();
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    50
        updater.start();
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    51
        Retriever [] retrievers = new Retriever[NUMBER_OF_RETRIEVERS];
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    52
        for (int i=0; i<retrievers.length; i++) {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    53
            retrievers[i] = new Retriever();
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    54
            retrievers[i].start();
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    55
        }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    56
        Thread.sleep(TEST_TIME_MS);
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    57
        System.out.println("Interrupt");
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    58
        updater.interrupt();
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    59
        updater.join();
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    60
        for (int i=0; i<retrievers.length; i++) {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    61
            retrievers[i].interrupt();
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    62
            retrievers[i].join();
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    63
        }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    64
        System.out.println("Done");
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    65
        if (testFailed) {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    66
            throw new Exception("Test Failed");
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    67
        }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    68
        System.out.println("Test Passed");
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    69
    }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    70
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    71
    private static class Updater extends Thread {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    72
        @Override
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    73
        public void run() {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    74
            while (!isInterrupted()) {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    75
                testProvider.put("CertificateFactory.Added", "MyCertificateFactory");
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    76
            }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    77
            System.out.println("Updater stopped");
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    78
        }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    79
    }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    80
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    81
    private static class Retriever extends Thread {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    82
        @Override
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    83
        public void run() {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    84
            while (!isInterrupted()) {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    85
                Provider.Service service = testProvider.getService("CertificateFactory", "Fixed");
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    86
                if (service == null) {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    87
                    if (!testFailed) {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    88
                        System.err.println("CertificateFactory.Fixed is NULL");
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    89
                        testFailed = true;
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    90
                    }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    91
                } else {
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    92
                    //System.out.println("CertificateFactory.Fixed is good");
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    93
                }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    94
            }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    95
            System.out.println("Retriever stopped");
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    96
        }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    97
    }
270abce77942 8231387: java.security.Provider.getService returns random result due to race condition with mutating methods in the same class
phh
parents:
diff changeset
    98
}