Skip to content

Commit

Permalink
[GR-51623] Call String::to{Upper,Lower}Case with Locale.ENGLISH.
Browse files Browse the repository at this point in the history
PullRequest: graal/16723
  • Loading branch information
fniephaus committed Jan 31, 2024
2 parents 6fdaeca + e4ddffd commit c94bb1c
Show file tree
Hide file tree
Showing 38 changed files with 185 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ public static void runTest(InvariantsTool tool) {
verifiers.add(new VerifyGraphUniqueUsages());
verifiers.add(new VerifyEndlessLoops());
verifiers.add(new VerifyPhaseNoDirectRecursion());
verifiers.add(new VerifyStringCaseUsage());
VerifyAssertionUsage assertionUsages = null;
boolean checkAssertions = tool.checkAssertions();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2024, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
package jdk.graal.compiler.core.test;

import jdk.graal.compiler.nodes.StructuredGraph;
import jdk.graal.compiler.nodes.java.MethodCallTargetNode;
import jdk.graal.compiler.nodes.spi.CoreProviders;
import jdk.graal.compiler.phases.VerifyPhase;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;

public class VerifyStringCaseUsage extends VerifyPhase<CoreProviders> {
private static final String TO_LOWER_CASE_METHOD_NAME = "toLowerCase";
private static final String TO_UPPER_CASE_METHOD_NAME = "toUpperCase";

@Override
protected void verify(StructuredGraph graph, CoreProviders context) {
// Ensure methods of interest still exist.
try {
String.class.getDeclaredMethod(TO_LOWER_CASE_METHOD_NAME);
String.class.getDeclaredMethod(TO_UPPER_CASE_METHOD_NAME);
} catch (NoSuchMethodException e) {
throw new VerificationError("Failed to find expected method. Has the String class been modified?", e);
}
// Find and check uses of methods of interest.
final ResolvedJavaType stringType = context.getMetaAccess().lookupJavaType(String.class);
for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
ResolvedJavaMethod callee = t.targetMethod();
if (callee.getDeclaringClass().equals(stringType)) {
if (callee.getParameters().length > 0) {
continue;
}
String calleeName = callee.getName();
if (calleeName.equals(TO_LOWER_CASE_METHOD_NAME) || calleeName.equals(TO_UPPER_CASE_METHOD_NAME)) {
throw new VerificationError(t, "call to parameterless %s is prohibited to avoid localization issues. Please pass a locale such as 'Locale.ENGLISH' explicitly.",
callee.format("%H.%n(%p)"));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Locale;

import jdk.graal.compiler.options.OptionValues;
import jdk.graal.compiler.serviceprovider.ServiceProvider;

import jdk.vm.ci.code.CodeCacheProvider;
import jdk.vm.ci.code.CodeUtil;
import jdk.vm.ci.code.CodeUtil.DefaultRefMapFormatter;
Expand Down Expand Up @@ -154,7 +154,7 @@ static class HexCodeFileDisTool {
arch = "amd64";
}
int wordWidth = arch.endsWith("64") ? 64 : Integer.parseInt(Services.getSavedProperty("sun.arch.data.model", "64"));
String hcf = new HexCodeFile(code, 0L, arch.toLowerCase(), wordWidth).toEmbeddedString();
String hcf = new HexCodeFile(code, 0L, arch.toLowerCase(Locale.ENGLISH), wordWidth).toEmbeddedString();
try {
toolMethod.invokeExact(hcf);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.Set;

Expand Down Expand Up @@ -210,7 +211,7 @@ public int getPhaseSkipOdds() {
@Override
public String toString() {
return String.format("%s%n%s%nProbability of inserting a phase: 1/%s (-Dtest.graal.skip.phase.insertion.odds.%s=%s)%n",
super.toString(), ignoredPhasesToString(), phaseSkipOdds, String.join(".", getTierName().toLowerCase().split(" ")), phaseSkipOdds);
super.toString(), ignoredPhasesToString(), phaseSkipOdds, String.join(".", getTierName().toLowerCase(Locale.ENGLISH).split(" ")), phaseSkipOdds);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.function.Supplier;

import jdk.graal.compiler.debug.GraalError;
import jdk.graal.compiler.core.phases.fuzzing.AbstractCompilationPlan.PrintingUtils;
import jdk.graal.compiler.debug.GraalError;
import jdk.graal.compiler.nodes.GraphState;
import jdk.graal.compiler.nodes.GraphState.StageFlag;
import jdk.graal.compiler.phases.BasePhase;
Expand Down Expand Up @@ -303,7 +304,7 @@ protected String ignoredPhasesToString() {
return "Every phase that was given is in the resulting tier plan.";
}
Formatter formatter = new Formatter();
formatter.format("Phase%s in %s ignored by the fuzzer:%n", getIgnoredPhases().size() > 1 ? "s" : "", getTierName().toLowerCase());
formatter.format("Phase%s in %s ignored by the fuzzer:%n", getIgnoredPhases().size() > 1 ? "s" : "", getTierName().toLowerCase(Locale.ENGLISH));
for (BasePhase<? super C> phase : getIgnoredPhases()) {
if (phase instanceof PhaseSuite) {
formatter.format("%s", PrintingUtils.indent(phase.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
Expand All @@ -48,11 +49,11 @@
import org.graalvm.collections.EconomicMap;
import org.graalvm.collections.EconomicSet;
import org.graalvm.collections.Pair;

import jdk.graal.compiler.graphio.GraphOutput;
import jdk.graal.compiler.options.OptionKey;
import jdk.graal.compiler.options.OptionValues;
import jdk.graal.compiler.serviceprovider.GraalServices;
import jdk.graal.compiler.graphio.GraphOutput;

import jdk.vm.ci.common.NativeImageReinitialize;
import jdk.vm.ci.meta.JavaMethod;
import jdk.vm.ci.meta.ResolvedJavaMethod;
Expand Down Expand Up @@ -1895,7 +1896,7 @@ public static String applyFormattingFlagsAndWidth(String s, int flags, int width

String res = sb.toString();
if ((flags & UPPERCASE) == UPPERCASE) {
res = res.toUpperCase();
res = res.toUpperCase(Locale.ENGLISH);
}
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Locale;

import jdk.graal.compiler.core.common.Fields;
import jdk.graal.compiler.core.common.FieldsScanner;
Expand Down Expand Up @@ -322,7 +323,7 @@ String toString(LIRInstruction obj) {
StringBuilder result = new StringBuilder();

appendValues(result, obj, "", " = ", "(", ")", new String[]{""}, defs);
result.append(String.valueOf(getOpcode(obj)).toUpperCase());
result.append(String.valueOf(getOpcode(obj)).toUpperCase(Locale.ENGLISH));
appendValues(result, obj, " ", "", "(", ")", new String[]{"", "~"}, uses, alives);
appendValues(result, obj, " ", "", "{", "}", new String[]{""}, temps);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.EnumSet;
import java.util.Formatter;
import java.util.Locale;
import java.util.Objects;

import jdk.graal.compiler.api.replacements.Snippet;
Expand Down Expand Up @@ -773,7 +774,7 @@ public enum MandatoryStages {
* is found, returns {@link #COMMUNITY}.
*/
public static MandatoryStages getFromName(String name) {
switch (name.toLowerCase()) {
switch (name.toLowerCase(Locale.ENGLISH)) {
case "economy":
return ECONOMY;
case "community":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Collection;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;
import java.util.ServiceLoader;

import org.graalvm.collections.EconomicMap;
Expand Down Expand Up @@ -211,7 +212,7 @@ public static Object parseOptionValue(OptionDescriptor desc, Object uncheckedVal
}

private static long parseLong(String v) {
String valueString = v.toLowerCase();
String valueString = v.toLowerCase(Locale.ENGLISH);
long scale = 1;
if (valueString.endsWith("k")) {
scale = 1024L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -154,7 +155,7 @@ private void initClosure(IntrinsificationTarget target) {
this.envPackage = target.envPackage;
this.envClassName = target.envClassName;

this.envName = envClassName.toLowerCase();
this.envName = envClassName.toLowerCase(Locale.ENGLISH);
this.imports = envPackage + "." + envClassName + ";\n";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;

import org.graalvm.nativeimage.ImageInfo;
Expand Down Expand Up @@ -262,7 +263,7 @@ public enum SpecComplianceMode {
@Override
public SpecComplianceMode apply(String s) {
try {
return SpecComplianceMode.valueOf(s.toUpperCase());
return SpecComplianceMode.valueOf(s.toUpperCase(Locale.ENGLISH));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("--java.SpecCompliance: Mode can be 'strict' or 'hotspot'.");
}
Expand All @@ -285,7 +286,7 @@ public enum VerifyMode {
@Override
public VerifyMode apply(String s) {
try {
return VerifyMode.valueOf(s.toUpperCase());
return VerifyMode.valueOf(s.toUpperCase(Locale.ENGLISH));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("-Xverify: Mode can be 'none', 'remote' or 'all'.");
}
Expand Down Expand Up @@ -341,7 +342,7 @@ public LivenessAnalysisMode apply(String s) {
return LivenessAnalysisMode.NONE;
}
try {
return LivenessAnalysisMode.valueOf(s.toUpperCase());
return LivenessAnalysisMode.valueOf(s.toUpperCase(Locale.ENGLISH));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("--java.LivenessAnalysis can only be 'none'|'false', 'auto' or 'all'|'true'.");
}
Expand Down Expand Up @@ -638,7 +639,7 @@ public enum JImageMode {
@Override
public JImageMode apply(String s) {
try {
return JImageMode.valueOf(s.toUpperCase());
return JImageMode.valueOf(s.toUpperCase(Locale.ENGLISH));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("JImage: Mode can be 'native', 'java'.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -46,6 +46,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -61,7 +62,7 @@ public class Main {
static {
String logLevel = System.getenv(LOGGING_PROP);
if (logLevel != null) {
Level level = Level.parse(logLevel.toUpperCase());
Level level = Level.parse(logLevel.toUpperCase(Locale.ENGLISH));
LOGGER.setLevel(level);
Handler[] handlers = Logger.getLogger("").getHandlers();
for (Handler h : handlers) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -47,6 +47,7 @@
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
Expand Down Expand Up @@ -1079,7 +1080,7 @@ private static DeclaredType getComponentType(TypeMirror arrayType, Types types)
}

private static String cacheEntryName(CharSequence name) {
return name.toString().replace('.', '_').toUpperCase() + "_BINARY_NAME";
return name.toString().replace('.', '_').toUpperCase(Locale.ENGLISH) + "_BINARY_NAME";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import jdk.graal.compiler.options.Option;
import jdk.graal.compiler.options.OptionKey;

import java.util.Locale;

public class PointstoOptions {

@Option(help = "Track primitive values using the infrastructure of points-to analysis.")//
Expand Down Expand Up @@ -168,7 +170,7 @@ protected void onValueUpdate(EconomicMap<OptionKey<?>, Object> values, Boolean o
public static final OptionKey<String> AnalysisContextSensitivity = new OptionKey<>("insens") {
@Override
protected void onValueUpdate(EconomicMap<OptionKey<?>, Object> values, String oldValue, String newValue) {
switch (newValue.toLowerCase()) {
switch (newValue.toLowerCase(Locale.ENGLISH)) {
case "insens":
AllocationSiteSensitiveHeap.update(values, false);
MinHeapContextDepth.update(values, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package com.oracle.objectfile;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.oracle.objectfile.ObjectFile.Format;
Expand Down Expand Up @@ -125,7 +126,7 @@ public String getSegmentName(ObjectFile.Format f) {
// default implementation
switch (f) {
case MACH_O:
return getFormatDependentName(f).toUpperCase();
return getFormatDependentName(f).toUpperCase(Locale.ENGLISH);
default:
case ELF:
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.oracle.objectfile.ObjectFile.RelocationMethod;
import com.oracle.objectfile.elf.ELFRelocationSection.ELFRelocationMethod;

import java.util.Locale;

/**
* ELF machine type (incomplete). Each machine type also defines its set of relocation types.
*/
Expand Down Expand Up @@ -85,7 +87,7 @@ int flags() {
abstract int flags();

public static ELFMachine from(String s) {
switch (s.toLowerCase()) {
switch (s.toLowerCase(Locale.ENGLISH)) {
case "amd64":
case "x86_64":
return X86_64;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.graalvm.nativeimage.c.function.CFunction.Transition.NO_TRANSITION;

import java.util.Hashtable;
import java.util.Locale;

import org.graalvm.nativeimage.c.function.CFunction;
import org.graalvm.word.WordFactory;
Expand Down Expand Up @@ -117,7 +118,7 @@ static void ensureInitialized() {
if (signalDispatcherThread.getState() == Thread.State.NEW) {
if (!jdkMiscSignalInit()) {
VMError.shouldNotReachHere("Native state initialization for jdk.internal.misc.Signal failed with error code: 0x" +
Integer.toUnsignedString(WinBase.GetLastError(), 16).toUpperCase());
Integer.toUnsignedString(WinBase.GetLastError(), 16).toUpperCase(Locale.ENGLISH));
}
RuntimeSupport.getRuntimeSupport().addTearDownHook(isFirstIsolate -> osTerminateSignalThread());
signalDispatcherThread.start();
Expand Down
Loading

0 comments on commit c94bb1c

Please sign in to comment.