Skip to content

Commit

Permalink
Merge master HEAD into openj9-staging
Browse files Browse the repository at this point in the history
Signed-off-by: J9 Build <j9build@ca.ibm.com>
  • Loading branch information
j9build committed Jan 3, 2024
2 parents c4aea1b + bbb6d8b commit 1359ca0
Show file tree
Hide file tree
Showing 21 changed files with 608 additions and 157 deletions.
28 changes: 25 additions & 3 deletions src/java.base/share/classes/java/io/BufferedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -643,9 +643,13 @@ private long implTransferTo(OutputStream out) throws IOException {
if (getClass() == BufferedInputStream.class && markpos == -1) {
int avail = count - pos;
if (avail > 0) {
// Prevent poisoning and leaking of buf
byte[] buffer = Arrays.copyOfRange(getBufIfOpen(), pos, count);
out.write(buffer);
if (isTrusted(out)) {
out.write(getBufIfOpen(), pos, count);
} else {
// Prevent poisoning and leaking of buf
byte[] buffer = Arrays.copyOfRange(getBufIfOpen(), pos, count);
out.write(buffer);
}
pos = count;
}
try {
Expand All @@ -658,4 +662,22 @@ private long implTransferTo(OutputStream out) throws IOException {
}
}

/**
* Returns true if this class satisfies the following conditions:
* <ul>
* <li>does not retain a reference to the {@code byte[]}</li>
* <li>does not leak a reference to the {@code byte[]} to non-trusted classes</li>
* <li>does not modify the contents of the {@code byte[]}</li>
* <li>{@code write()} method does not read the contents outside of the offset/length bounds</li>
* </ul>
*
* @return true if this class is trusted
*/
private static boolean isTrusted(OutputStream os) {
var clazz = os.getClass();
return clazz == ByteArrayOutputStream.class
|| clazz == FileOutputStream.class
|| clazz == PipedOutputStream.class;
}

}
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/util/IdentityHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private void init(int initCapacity) {
}

/**
* Constructs a new identity hash map containing the keys-value mappings
* Constructs a new identity hash map containing the key-value mappings
* in the specified map.
*
* @param m the map whose mappings are to be placed into this map
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 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
Expand Down Expand Up @@ -189,45 +189,73 @@ public BootstrapMethodEntryImpl bootstrapMethodEntry(int index) {
return bsmEntries().get(index);
}

private static IllegalArgumentException outOfBoundsError(IndexOutOfBoundsException cause) {
return new IllegalArgumentException("Reading beyond classfile bounds", cause);
}

@Override
public int readU1(int p) {
return buffer[p] & 0xFF;
try {
return buffer[p] & 0xFF;
} catch (IndexOutOfBoundsException e) {
throw outOfBoundsError(e);
}
}

@Override
public int readU2(int p) {
int b1 = buffer[p] & 0xFF;
int b2 = buffer[p + 1] & 0xFF;
return (b1 << 8) + b2;
try {
int b1 = buffer[p] & 0xFF;
int b2 = buffer[p + 1] & 0xFF;
return (b1 << 8) + b2;
} catch (IndexOutOfBoundsException e) {
throw outOfBoundsError(e);
}
}

@Override
public int readS1(int p) {
return buffer[p];
try {
return buffer[p];
} catch (IndexOutOfBoundsException e) {
throw outOfBoundsError(e);
}
}

@Override
public int readS2(int p) {
int b1 = buffer[p];
int b2 = buffer[p + 1] & 0xFF;
return (b1 << 8) + b2;
try {
int b1 = buffer[p];
int b2 = buffer[p + 1] & 0xFF;
return (b1 << 8) + b2;
} catch (IndexOutOfBoundsException e) {
throw outOfBoundsError(e);
}
}

@Override
public int readInt(int p) {
int ch1 = buffer[p] & 0xFF;
int ch2 = buffer[p + 1] & 0xFF;
int ch3 = buffer[p + 2] & 0xFF;
int ch4 = buffer[p + 3] & 0xFF;
return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + ch4;
try {
int ch1 = buffer[p] & 0xFF;
int ch2 = buffer[p + 1] & 0xFF;
int ch3 = buffer[p + 2] & 0xFF;
int ch4 = buffer[p + 3] & 0xFF;
return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + ch4;
} catch (IndexOutOfBoundsException e) {
throw outOfBoundsError(e);
}
}

@Override
public long readLong(int p) {
return ((long) buffer[p + 0] << 56) + ((long) (buffer[p + 1] & 255) << 48) +
((long) (buffer[p + 2] & 255) << 40) + ((long) (buffer[p + 3] & 255) << 32) +
((long) (buffer[p + 4] & 255) << 24) + ((buffer[p + 5] & 255) << 16) + ((buffer[p + 6] & 255) << 8) +
(buffer[p + 7] & 255);
try {
return ((long) buffer[p + 0] << 56) + ((long) (buffer[p + 1] & 255) << 48) +
((long) (buffer[p + 2] & 255) << 40) + ((long) (buffer[p + 3] & 255) << 32) +
((long) (buffer[p + 4] & 255) << 24) + ((buffer[p + 5] & 255) << 16) + ((buffer[p + 6] & 255) << 8) +
(buffer[p + 7] & 255);
} catch (IndexOutOfBoundsException e) {
throw outOfBoundsError(e);
}
}

@Override
Expand All @@ -242,12 +270,20 @@ public double readDouble(int p) {

@Override
public byte[] readBytes(int p, int len) {
return Arrays.copyOfRange(buffer, p, p + len);
try {
return Arrays.copyOfRange(buffer, p, p + len);
} catch (IndexOutOfBoundsException e) {
throw outOfBoundsError(e);
}
}

@Override
public void copyBytesTo(BufWriter buf, int p, int len) {
buf.writeBytes(buffer, p, len);
try {
buf.writeBytes(buffer, p, len);
} catch (IndexOutOfBoundsException e) {
throw outOfBoundsError(e);
}
}

BootstrapMethodsAttribute bootstrapMethodsAttribute() {
Expand Down Expand Up @@ -446,8 +482,12 @@ public boolean compare(BufWriter bufWriter,
int bufWriterOffset,
int classReaderOffset,
int length) {
return Arrays.equals(((BufWriterImpl) bufWriter).elems,
bufWriterOffset, bufWriterOffset + length,
buffer, classReaderOffset, classReaderOffset + length);
try {
return Arrays.equals(((BufWriterImpl) bufWriter).elems,
bufWriterOffset, bufWriterOffset + length,
buffer, classReaderOffset, classReaderOffset + length);
} catch (IndexOutOfBoundsException e) {
throw outOfBoundsError(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 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
Expand Down Expand Up @@ -44,49 +44,64 @@ public SignaturesImpl() {
public ClassSignature parseClassSignature(String signature) {
this.sig = signature;
sigp = 0;
List<TypeParam> typeParamTypes = parseParamTypes();
RefTypeSig superclass = referenceTypeSig();
ArrayList<RefTypeSig> superinterfaces = null;
while (sigp < sig.length()) {
if (superinterfaces == null)
superinterfaces = new ArrayList<>();
superinterfaces.add(referenceTypeSig());
try {
List<TypeParam> typeParamTypes = parseParamTypes();
RefTypeSig superclass = referenceTypeSig();
ArrayList<RefTypeSig> superinterfaces = null;
while (sigp < sig.length()) {
if (superinterfaces == null)
superinterfaces = new ArrayList<>();
superinterfaces.add(referenceTypeSig());
}
return new ClassSignatureImpl(typeParamTypes, superclass, null2Empty(superinterfaces));
} catch (IndexOutOfBoundsException e) {
throw error("Not a valid class signature");
}
return new ClassSignatureImpl(typeParamTypes, superclass, null2Empty(superinterfaces));
}

public MethodSignature parseMethodSignature(String signature) {
this.sig = signature;
sigp = 0;
List<TypeParam> typeParamTypes = parseParamTypes();
assert sig.charAt(sigp) == '(';
sigp++;
ArrayList<Signature> paramTypes = null;
while (sig.charAt(sigp) != ')') {
if (paramTypes == null)
paramTypes = new ArrayList<>();
paramTypes.add(typeSig());
}
sigp++;
Signature returnType = typeSig();
ArrayList<ThrowableSig> throwsTypes = null;
while (sigp < sig.length() && sig.charAt(sigp) == '^') {
try {
List<TypeParam> typeParamTypes = parseParamTypes();
if (sig.charAt(sigp) != '(') throw error("Expected ( at possition %d of signature".formatted(sigp));
sigp++;
ArrayList<Signature> paramTypes = null;
while (sig.charAt(sigp) != ')') {
if (paramTypes == null)
paramTypes = new ArrayList<>();
paramTypes.add(typeSig());
}
sigp++;
if (throwsTypes == null)
throwsTypes = new ArrayList<>();
var t = typeSig();
if (t instanceof ThrowableSig ts)
throwsTypes.add(ts);
else
throw new IllegalArgumentException("not a valid type signature: " + sig);
Signature returnType = typeSig();
ArrayList<ThrowableSig> throwsTypes = null;
while (sigp < sig.length()) {
if (sig.charAt(sigp) != '^') throw error("Expected ^ at possition %d of signature".formatted(sigp));
sigp++;
if (throwsTypes == null)
throwsTypes = new ArrayList<>();
var t = referenceTypeSig();
if (t instanceof ThrowableSig ts)
throwsTypes.add(ts);
else
throw error("Not a valid throwable signature %s in".formatted(t.signatureString()));
}
return new MethodSignatureImpl(typeParamTypes, null2Empty(throwsTypes), returnType, null2Empty(paramTypes));
} catch (IndexOutOfBoundsException e) {
throw error("Not a valid method signature");
}
return new MethodSignatureImpl(typeParamTypes, null2Empty(throwsTypes), returnType, null2Empty(paramTypes));
}

public Signature parseSignature(String signature) {
this.sig = signature;
sigp = 0;
return typeSig();
try {
var s = typeSig();
if (sigp == signature.length())
return s;
} catch (IndexOutOfBoundsException e) {
}
throw error("Not a valid type signature");
}

private List<TypeParam> parseParamTypes() {
Expand Down Expand Up @@ -157,7 +172,7 @@ private RefTypeSig referenceTypeSig() {
return ty;
case '[': return ArrayTypeSig.of(typeSig());
}
throw new IllegalArgumentException("not a valid type signature: " + sig);
throw error("Unexpected character %c at possition %d of signature".formatted(c, sigp - 1));
}

private TypeArg typeArg() {
Expand Down Expand Up @@ -292,4 +307,8 @@ public String signatureString() {
private static <T> List<T> null2Empty(ArrayList<T> l) {
return l == null ? List.of() : Collections.unmodifiableList(l);
}

private IllegalArgumentException error(String message) {
return new IllegalArgumentException("%s: %s".formatted(message, sig));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 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
Expand Down Expand Up @@ -169,7 +169,7 @@ private ElementFilter() {} // Do not instantiate.
}

/**
* {@return a set of types in {@code elements}}
* {@return a set of classes and interfaces in {@code elements}}
* @param elements the elements to filter
*/
public static Set<TypeElement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public XMLStreamException(String msg, Throwable th) {
public XMLStreamException(String msg, Location location, Throwable th) {
super("ParseError at [row,col]:["+location.getLineNumber()+","+
location.getColumnNumber()+"]\n"+
"Message: "+msg);
"Message: "+msg, th);
nested = th;
this.location = location;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public void visitMethodDef(JCMethodDecl tree) {
public void visitApply(JCMethodInvocation invoke) {

// Get method symbol
MethodSymbol sym = (MethodSymbol)TreeInfo.symbolFor(invoke.meth);
Symbol sym = TreeInfo.symbolFor(invoke.meth);

// Recurse on method expression
scan(invoke.meth);
Expand All @@ -530,7 +530,7 @@ public void visitApply(JCMethodInvocation invoke) {
invoke(invoke, sym, invoke.args, receiverRefs);
}

private void invoke(JCTree site, MethodSymbol sym, List<JCExpression> args, RefSet<?> receiverRefs) {
private void invoke(JCTree site, Symbol sym, List<JCExpression> args, RefSet<?> receiverRefs) {

// Skip if ignoring warnings for a constructor invoked via 'this()'
if (suppressed.contains(sym))
Expand Down Expand Up @@ -810,6 +810,10 @@ public void visitSelect(JCFieldAccess tree) {

@Override
public void visitReference(JCMemberReference tree) {
if (tree.type.isErroneous()) {
//error recovery - ignore erroneous member references
return ;
}

// Scan target expression and extract 'this' references, if any
scan(tree.expr);
Expand Down
Loading

0 comments on commit 1359ca0

Please sign in to comment.