8374582: [REDO] Move input validation checks to Java for java.lang.StringCoding intrinsics

Co-authored-by: Volkan Yazici <vyazici@openjdk.org>
Reviewed-by: chagedorn, thartmann, vyazici
This commit is contained in:
Damon Fenacci 2026-02-06 07:44:25 +00:00
parent 90f370235c
commit bfd5bdaf7f
36 changed files with 442 additions and 155 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024, Red Hat Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
@ -6419,10 +6419,14 @@ void MacroAssembler::fill_words(Register base, Register cnt, Register value)
// Intrinsic for
//
// - sun/nio/cs/ISO_8859_1$Encoder.implEncodeISOArray
// return the number of characters copied.
// - java/lang/StringUTF16.compress
// return index of non-latin1 character if copy fails, otherwise 'len'.
// - sun.nio.cs.ISO_8859_1.Encoder#encodeISOArray0(byte[] sa, int sp, byte[] da, int dp, int len)
// Encodes char[] to byte[] in ISO-8859-1
//
// - java.lang.StringCoding#encodeISOArray0(byte[] sa, int sp, byte[] da, int dp, int len)
// Encodes byte[] (containing UTF-16) to byte[] in ISO-8859-1
//
// - java.lang.StringCoding#encodeAsciiArray0(char[] sa, int sp, byte[] da, int dp, int len)
// Encodes char[] to byte[] in ASCII
//
// This version always returns the number of characters copied, and does not
// clobber the 'len' register. A successful copy will complete with the post-

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
@ -2813,10 +2813,14 @@ void C2_MacroAssembler::char_array_compress_v(Register src, Register dst, Regist
// Intrinsic for
//
// - sun/nio/cs/ISO_8859_1$Encoder.implEncodeISOArray
// return the number of characters copied.
// - java/lang/StringUTF16.compress
// return index of non-latin1 character if copy fails, otherwise 'len'.
// - sun.nio.cs.ISO_8859_1.Encoder#encodeISOArray0(byte[] sa, int sp, byte[] da, int dp, int len)
// Encodes char[] to byte[] in ISO-8859-1
//
// - java.lang.StringCoding#encodeISOArray0(byte[] sa, int sp, byte[] da, int dp, int len)
// Encodes byte[] (containing UTF-16) to byte[] in ISO-8859-1
//
// - java.lang.StringCoding#encodeAsciiArray0(char[] sa, int sp, byte[] da, int dp, int len)
// Encodes char[] to byte[] in ASCII
//
// This version always returns the number of characters copied. A successful
// copy will complete with the post-condition: 'res' == 'len', while an

View File

@ -6251,32 +6251,46 @@ void MacroAssembler::evpbroadcast(BasicType type, XMMRegister dst, Register src,
}
}
// encode char[] to byte[] in ISO_8859_1 or ASCII
//@IntrinsicCandidate
//private static int implEncodeISOArray(byte[] sa, int sp,
//byte[] da, int dp, int len) {
// int i = 0;
// for (; i < len; i++) {
// char c = StringUTF16.getChar(sa, sp++);
// if (c > '\u00FF')
// break;
// da[dp++] = (byte)c;
// }
// return i;
//}
//
//@IntrinsicCandidate
//private static int implEncodeAsciiArray(char[] sa, int sp,
// byte[] da, int dp, int len) {
// int i = 0;
// for (; i < len; i++) {
// char c = sa[sp++];
// if (c >= '\u0080')
// break;
// da[dp++] = (byte)c;
// }
// return i;
//}
// Encode given char[]/byte[] to byte[] in ISO_8859_1 or ASCII
//
// @IntrinsicCandidate
// int sun.nio.cs.ISO_8859_1.Encoder#encodeISOArray0(
// char[] sa, int sp, byte[] da, int dp, int len) {
// int i = 0;
// for (; i < len; i++) {
// char c = sa[sp++];
// if (c > '\u00FF')
// break;
// da[dp++] = (byte) c;
// }
// return i;
// }
//
// @IntrinsicCandidate
// int java.lang.StringCoding.encodeISOArray0(
// byte[] sa, int sp, byte[] da, int dp, int len) {
// int i = 0;
// for (; i < len; i++) {
// char c = StringUTF16.getChar(sa, sp++);
// if (c > '\u00FF')
// break;
// da[dp++] = (byte) c;
// }
// return i;
// }
//
// @IntrinsicCandidate
// int java.lang.StringCoding.encodeAsciiArray0(
// char[] sa, int sp, byte[] da, int dp, int len) {
// int i = 0;
// for (; i < len; i++) {
// char c = sa[sp++];
// if (c >= '\u0080')
// break;
// da[dp++] = (byte) c;
// }
// return i;
// }
void MacroAssembler::encode_iso_array(Register src, Register dst, Register len,
XMMRegister tmp1Reg, XMMRegister tmp2Reg,
XMMRegister tmp3Reg, XMMRegister tmp4Reg,

View File

@ -415,18 +415,18 @@ class methodHandle;
\
do_class(java_lang_StringCoding, "java/lang/StringCoding") \
do_intrinsic(_countPositives, java_lang_StringCoding, countPositives_name, countPositives_signature, F_S) \
do_name( countPositives_name, "countPositives") \
do_name( countPositives_name, "countPositives0") \
do_signature(countPositives_signature, "([BII)I") \
\
do_class(sun_nio_cs_iso8859_1_Encoder, "sun/nio/cs/ISO_8859_1$Encoder") \
do_intrinsic(_encodeISOArray, sun_nio_cs_iso8859_1_Encoder, encodeISOArray_name, encodeISOArray_signature, F_S) \
do_name( encodeISOArray_name, "implEncodeISOArray") \
do_name( encodeISOArray_name, "encodeISOArray0") \
do_signature(encodeISOArray_signature, "([CI[BII)I") \
\
do_intrinsic(_encodeByteISOArray, java_lang_StringCoding, encodeISOArray_name, indexOfI_signature, F_S) \
\
do_intrinsic(_encodeAsciiArray, java_lang_StringCoding, encodeAsciiArray_name, encodeISOArray_signature, F_S) \
do_name( encodeAsciiArray_name, "implEncodeAsciiArray") \
do_name( encodeAsciiArray_name, "encodeAsciiArray0") \
\
do_class(java_math_BigInteger, "java/math/BigInteger") \
do_intrinsic(_multiplyToLen, java_math_BigInteger, multiplyToLen_name, multiplyToLen_signature, F_S) \

View File

@ -281,7 +281,7 @@ macro(OpaqueLoopInit)
macro(OpaqueLoopStride)
macro(OpaqueMultiversioning)
macro(OpaqueZeroTripGuard)
macro(OpaqueNotNull)
macro(OpaqueConstantBool)
macro(OpaqueInitializedAssertionPredicate)
macro(OpaqueTemplateAssertionPredicate)
macro(ProfileBoolean)

View File

@ -588,7 +588,7 @@ bool ConnectionGraph::can_reduce_check_users(Node* n, uint nesting) const {
// CmpP/N used by the If controlling the cast.
if (use->in(0)->is_IfTrue() || use->in(0)->is_IfFalse()) {
Node* iff = use->in(0)->in(0);
// We may have an OpaqueNotNull node between If and Bool nodes. But we could also have a sub class of IfNode,
// We may have an OpaqueConstantBool node between If and Bool nodes. But we could also have a sub class of IfNode,
// for example, an OuterStripMinedLoopEnd or a Parse Predicate. Bail out in all these cases.
bool can_reduce = (iff->Opcode() == Op_If) && iff->in(1)->is_Bool() && iff->in(1)->in(1)->is_Cmp();
if (can_reduce) {

View File

@ -1472,7 +1472,7 @@ Node* GraphKit::cast_not_null(Node* obj, bool do_replace_in_map) {
// In that case that data path will die and we need the control path
// to become dead as well to keep the graph consistent. So we have to
// add a check for null for which one branch can't be taken. It uses
// an OpaqueNotNull node that will cause the check to be removed after loop
// an OpaqueConstantBool node that will cause the check to be removed after loop
// opts so the test goes away and the compiled code doesn't execute a
// useless check.
Node* GraphKit::must_be_not_null(Node* value, bool do_replace_in_map) {
@ -1481,7 +1481,7 @@ Node* GraphKit::must_be_not_null(Node* value, bool do_replace_in_map) {
}
Node* chk = _gvn.transform(new CmpPNode(value, null()));
Node* tst = _gvn.transform(new BoolNode(chk, BoolTest::ne));
Node* opaq = _gvn.transform(new OpaqueNotNullNode(C, tst));
Node* opaq = _gvn.transform(new OpaqueConstantBoolNode(C, tst, true));
IfNode* iff = new IfNode(control(), opaq, PROB_MAX, COUNT_UNKNOWN);
_gvn.set_type(iff, iff->Value(&_gvn));
if (!tst->is_Con()) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2026, 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
@ -891,13 +891,16 @@ inline Node* LibraryCallKit::generate_fair_guard(Node* test, RegionNode* region)
}
inline Node* LibraryCallKit::generate_negative_guard(Node* index, RegionNode* region,
Node* *pos_index) {
Node** pos_index, bool with_opaque) {
if (stopped())
return nullptr; // already stopped
if (_gvn.type(index)->higher_equal(TypeInt::POS)) // [0,maxint]
return nullptr; // index is already adequately typed
Node* cmp_lt = _gvn.transform(new CmpINode(index, intcon(0)));
Node* bol_lt = _gvn.transform(new BoolNode(cmp_lt, BoolTest::lt));
if (with_opaque) {
bol_lt = _gvn.transform(new OpaqueConstantBoolNode(C, bol_lt, false));
}
Node* is_neg = generate_guard(bol_lt, region, PROB_MIN);
if (is_neg != nullptr && pos_index != nullptr) {
// Emulate effect of Parse::adjust_map_after_if.
@ -924,7 +927,8 @@ inline Node* LibraryCallKit::generate_negative_guard(Node* index, RegionNode* re
inline Node* LibraryCallKit::generate_limit_guard(Node* offset,
Node* subseq_length,
Node* array_length,
RegionNode* region) {
RegionNode* region,
bool with_opaque) {
if (stopped())
return nullptr; // already stopped
bool zero_offset = _gvn.type(offset) == TypeInt::ZERO;
@ -935,12 +939,19 @@ inline Node* LibraryCallKit::generate_limit_guard(Node* offset,
last = _gvn.transform(new AddINode(last, offset));
Node* cmp_lt = _gvn.transform(new CmpUNode(array_length, last));
Node* bol_lt = _gvn.transform(new BoolNode(cmp_lt, BoolTest::lt));
if (with_opaque) {
bol_lt = _gvn.transform(new OpaqueConstantBoolNode(C, bol_lt, false));
}
Node* is_over = generate_guard(bol_lt, region, PROB_MIN);
return is_over;
}
// Emit range checks for the given String.value byte array
void LibraryCallKit::generate_string_range_check(Node* array, Node* offset, Node* count, bool char_count) {
void LibraryCallKit::generate_string_range_check(Node* array,
Node* offset,
Node* count,
bool char_count,
bool halt_on_oob) {
if (stopped()) {
return; // already stopped
}
@ -952,16 +963,23 @@ void LibraryCallKit::generate_string_range_check(Node* array, Node* offset, Node
}
// Offset and count must not be negative
generate_negative_guard(offset, bailout);
generate_negative_guard(count, bailout);
generate_negative_guard(offset, bailout, nullptr, halt_on_oob);
generate_negative_guard(count, bailout, nullptr, halt_on_oob);
// Offset + count must not exceed length of array
generate_limit_guard(offset, count, load_array_length(array), bailout);
generate_limit_guard(offset, count, load_array_length(array), bailout, halt_on_oob);
if (bailout->req() > 1) {
PreserveJVMState pjvms(this);
set_control(_gvn.transform(bailout));
uncommon_trap(Deoptimization::Reason_intrinsic,
Deoptimization::Action_maybe_recompile);
if (halt_on_oob) {
bailout = _gvn.transform(bailout)->as_Region();
Node* frame = _gvn.transform(new ParmNode(C->start(), TypeFunc::FramePtr));
Node* halt = _gvn.transform(new HaltNode(bailout, frame, "unexpected guard failure in intrinsic"));
C->root()->add_req(halt);
} else {
PreserveJVMState pjvms(this);
set_control(_gvn.transform(bailout));
uncommon_trap(Deoptimization::Reason_intrinsic,
Deoptimization::Action_maybe_recompile);
}
}
}
@ -1119,6 +1137,7 @@ bool LibraryCallKit::inline_array_equals(StrIntrinsicNode::ArgEnc ae) {
//------------------------------inline_countPositives------------------------------
// int java.lang.StringCoding#countPositives0(byte[] ba, int off, int len)
bool LibraryCallKit::inline_countPositives() {
if (too_many_traps(Deoptimization::Reason_intrinsic)) {
return false;
@ -1131,12 +1150,11 @@ bool LibraryCallKit::inline_countPositives() {
Node* len = argument(2);
ba = must_be_not_null(ba, true);
// Range checks
generate_string_range_check(ba, offset, len, false);
generate_string_range_check(ba, offset, len, false, true);
if (stopped()) {
return true;
}
Node* ba_start = array_element_address(ba, offset, T_BYTE);
Node* result = new CountPositivesNode(control(), memory(TypeAryPtr::BYTES), ba_start, len);
set_result(_gvn.transform(result));
@ -6171,6 +6189,9 @@ CallStaticJavaNode* LibraryCallKit::get_uncommon_trap_from_success_proj(Node* no
}
//-------------inline_encodeISOArray-----------------------------------
// int sun.nio.cs.ISO_8859_1.Encoder#encodeISOArray0(byte[] sa, int sp, byte[] da, int dp, int len)
// int java.lang.StringCoding#encodeISOArray0(byte[] sa, int sp, byte[] da, int dp, int len)
// int java.lang.StringCoding#encodeAsciiArray0(char[] sa, int sp, byte[] da, int dp, int len)
// encode char[] to byte[] in ISO_8859_1 or ASCII
bool LibraryCallKit::inline_encodeISOArray(bool ascii) {
assert(callee()->signature()->size() == 5, "encodeISOArray has 5 parameters");
@ -6181,8 +6202,12 @@ bool LibraryCallKit::inline_encodeISOArray(bool ascii) {
Node *dst_offset = argument(3);
Node *length = argument(4);
// Cast source & target arrays to not-null
src = must_be_not_null(src, true);
dst = must_be_not_null(dst, true);
if (stopped()) {
return true;
}
const TypeAryPtr* src_type = src->Value(&_gvn)->isa_aryptr();
const TypeAryPtr* dst_type = dst->Value(&_gvn)->isa_aryptr();
@ -6199,6 +6224,13 @@ bool LibraryCallKit::inline_encodeISOArray(bool ascii) {
return false;
}
// Check source & target bounds
generate_string_range_check(src, src_offset, length, src_elem == T_BYTE, true);
generate_string_range_check(dst, dst_offset, length, false, true);
if (stopped()) {
return true;
}
Node* src_start = array_element_address(src, src_offset, T_CHAR);
Node* dst_start = array_element_address(dst, dst_offset, dst_elem);
// 'src_start' points to src array + scaled offset

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, 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
@ -158,12 +158,15 @@ class LibraryCallKit : public GraphKit {
Node* generate_fair_guard(Node* test, RegionNode* region);
Node* generate_negative_guard(Node* index, RegionNode* region,
// resulting CastII of index:
Node* *pos_index = nullptr);
Node** pos_index = nullptr,
bool with_opaque = false);
Node* generate_limit_guard(Node* offset, Node* subseq_length,
Node* array_length,
RegionNode* region);
RegionNode* region,
bool with_opaque = false);
void generate_string_range_check(Node* array, Node* offset,
Node* length, bool char_count);
Node* length, bool char_count,
bool halt_on_oob = false);
Node* current_thread_helper(Node* &tls_output, ByteSize handle_offset,
bool is_immutable);
Node* generate_current_thread(Node* &tls_output);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2026, 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
@ -1234,7 +1234,7 @@ bool IdealLoopTree::policy_range_check(PhaseIdealLoop* phase, bool provisional,
continue;
}
if (!bol->is_Bool()) {
assert(bol->is_OpaqueNotNull() ||
assert(bol->is_OpaqueConstantBool() ||
bol->is_OpaqueTemplateAssertionPredicate() ||
bol->is_OpaqueInitializedAssertionPredicate() ||
bol->is_OpaqueMultiversioning(),

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2026, 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
@ -1704,7 +1704,7 @@ void PhaseIdealLoop::try_sink_out_of_loop(Node* n) {
!n->is_Proj() &&
!n->is_MergeMem() &&
!n->is_CMove() &&
!n->is_OpaqueNotNull() &&
!n->is_OpaqueConstantBool() &&
!n->is_OpaqueInitializedAssertionPredicate() &&
!n->is_OpaqueTemplateAssertionPredicate() &&
!is_raw_to_oop_cast && // don't extend live ranges of raw oops
@ -2045,14 +2045,14 @@ Node* PhaseIdealLoop::clone_iff(PhiNode* phi) {
if (b->is_Phi()) {
_igvn.replace_input_of(phi, i, clone_iff(b->as_Phi()));
} else {
assert(b->is_Bool() || b->is_OpaqueNotNull() || b->is_OpaqueInitializedAssertionPredicate(),
"bool, non-null check with OpaqueNotNull or Initialized Assertion Predicate with its Opaque node");
assert(b->is_Bool() || b->is_OpaqueConstantBool() || b->is_OpaqueInitializedAssertionPredicate(),
"bool, non-null check with OpaqueConstantBool or Initialized Assertion Predicate with its Opaque node");
}
}
Node* n = phi->in(1);
Node* sample_opaque = nullptr;
Node *sample_bool = nullptr;
if (n->is_OpaqueNotNull() || n->is_OpaqueInitializedAssertionPredicate()) {
if (n->is_OpaqueConstantBool() || n->is_OpaqueInitializedAssertionPredicate()) {
sample_opaque = n;
sample_bool = n->in(1);
assert(sample_bool->is_Bool(), "wrong type");
@ -2228,7 +2228,7 @@ void PhaseIdealLoop::clone_loop_handle_data_uses(Node* old, Node_List &old_new,
// split if to break.
assert(!use->is_OpaqueTemplateAssertionPredicate(),
"should not clone a Template Assertion Predicate which should be removed once it's useless");
if (use->is_If() || use->is_CMove() || use->is_OpaqueNotNull() || use->is_OpaqueInitializedAssertionPredicate() ||
if (use->is_If() || use->is_CMove() || use->is_OpaqueConstantBool() || use->is_OpaqueInitializedAssertionPredicate() ||
(use->Opcode() == Op_AllocateArray && use->in(AllocateNode::ValidLengthTest) == old)) {
// Since this code is highly unlikely, we lazily build the worklist
// of such Nodes to go split.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2026, 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
@ -2499,7 +2499,7 @@ void PhaseMacroExpand::eliminate_macro_nodes() {
assert(n->Opcode() == Op_LoopLimit ||
n->Opcode() == Op_ModD ||
n->Opcode() == Op_ModF ||
n->is_OpaqueNotNull() ||
n->is_OpaqueConstantBool() ||
n->is_OpaqueInitializedAssertionPredicate() ||
n->Opcode() == Op_MaxL ||
n->Opcode() == Op_MinL ||
@ -2547,14 +2547,14 @@ void PhaseMacroExpand::eliminate_opaque_looplimit_macro_nodes() {
} else if (n->is_Opaque1()) {
_igvn.replace_node(n, n->in(1));
success = true;
} else if (n->is_OpaqueNotNull()) {
// Tests with OpaqueNotNull nodes are implicitly known to be true. Replace the node with true. In debug builds,
} else if (n->is_OpaqueConstantBool()) {
// Tests with OpaqueConstantBool nodes are implicitly known. Replace the node with true/false. In debug builds,
// we leave the test in the graph to have an additional sanity check at runtime. If the test fails (i.e. a bug),
// we will execute a Halt node.
#ifdef ASSERT
_igvn.replace_node(n, n->in(1));
#else
_igvn.replace_node(n, _igvn.intcon(1));
_igvn.replace_node(n, _igvn.intcon(n->as_OpaqueConstantBool()->constant()));
#endif
success = true;
} else if (n->is_OpaqueInitializedAssertionPredicate()) {

View File

@ -143,7 +143,7 @@ class Opaque1Node;
class OpaqueLoopInitNode;
class OpaqueLoopStrideNode;
class OpaqueMultiversioningNode;
class OpaqueNotNullNode;
class OpaqueConstantBoolNode;
class OpaqueInitializedAssertionPredicateNode;
class OpaqueTemplateAssertionPredicateNode;
class OuterStripMinedLoopNode;
@ -818,7 +818,7 @@ public:
DEFINE_CLASS_ID(OpaqueLoopInit, Opaque1, 0)
DEFINE_CLASS_ID(OpaqueLoopStride, Opaque1, 1)
DEFINE_CLASS_ID(OpaqueMultiversioning, Opaque1, 2)
DEFINE_CLASS_ID(OpaqueNotNull, Node, 17)
DEFINE_CLASS_ID(OpaqueConstantBool, Node, 17)
DEFINE_CLASS_ID(OpaqueInitializedAssertionPredicate, Node, 18)
DEFINE_CLASS_ID(OpaqueTemplateAssertionPredicate, Node, 19)
DEFINE_CLASS_ID(Move, Node, 20)
@ -1000,7 +1000,7 @@ public:
DEFINE_CLASS_QUERY(NegV)
DEFINE_CLASS_QUERY(NeverBranch)
DEFINE_CLASS_QUERY(Opaque1)
DEFINE_CLASS_QUERY(OpaqueNotNull)
DEFINE_CLASS_QUERY(OpaqueConstantBool)
DEFINE_CLASS_QUERY(OpaqueInitializedAssertionPredicate)
DEFINE_CLASS_QUERY(OpaqueTemplateAssertionPredicate)
DEFINE_CLASS_QUERY(OpaqueLoopInit)

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2026, 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
@ -108,10 +108,16 @@ void OpaqueMultiversioningNode::dump_spec(outputStream *st) const {
}
#endif
const Type* OpaqueNotNullNode::Value(PhaseGVN* phase) const {
const Type* OpaqueConstantBoolNode::Value(PhaseGVN* phase) const {
return phase->type(in(1));
}
#ifndef PRODUCT
void OpaqueConstantBoolNode::dump_spec(outputStream *st) const {
st->print(_constant ? " #true" : " #false");
}
#endif
OpaqueTemplateAssertionPredicateNode::OpaqueTemplateAssertionPredicateNode(BoolNode* bol, CountedLoopNode* loop_node)
: Node(nullptr, bol),
_loop_node(loop_node),

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2026, 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
@ -130,15 +130,26 @@ public:
// This node is used in the context of intrinsics. We sometimes implicitly know that an object is non-null even though
// the compiler cannot prove it. We therefore add a corresponding cast to propagate this implicit knowledge. However,
// this cast could become top during optimizations (input to cast becomes null) and the data path is folded. To ensure
// that the control path is also properly folded, we insert an If node with a OpaqueNotNullNode as condition. During
// macro expansion, we replace the OpaqueNotNullNodes with true in product builds such that the actually unneeded checks
// are folded and do not end up in the emitted code. In debug builds, we keep the actual checks as additional
// verification code (i.e. removing OpaqueNotNullNodes and use the BoolNode inputs instead). For more details, also see
// GraphKit::must_be_not_null().
class OpaqueNotNullNode : public Node {
// that the control path is also properly folded, we insert an If node with a OpaqueConstantBoolNode as condition.
// During macro expansion, we replace the OpaqueConstantBoolNodes with true in product builds such that the actually
// unneeded checks are folded and do not end up in the emitted code. In debug builds, we keep the actual checks as
// additional verification code (i.e. removing OpaqueConstantBoolNodes and use the BoolNode inputs instead). For more
// details, also see GraphKit::must_be_not_null().
// Similarly, sometimes we know that a size or limit guard is checked (e.g. there is already a guard in the caller) but
// the compiler cannot prove it. We could in principle avoid adding a guard in the intrinsic but in some cases (e.g.
// when the input is a constant that breaks the guard and the caller guard is not inlined) the input of the intrinsic
// can become top and the data path is folded. To ensure that the control path is also properly folded, we insert an
// OpaqueConstantBoolNode before the If node in the guard. During macro expansion, we replace the OpaqueConstantBoolNode
// with false in product builds such that the actually unneeded guards are folded and do not end up in the emitted code.
// In debug builds, we keep the actual checks as additional verification code (i.e. removing OpaqueConstantBoolNodes and
// use the BoolNode inputs instead).
class OpaqueConstantBoolNode : public Node {
private:
const bool _constant;
public:
OpaqueNotNullNode(Compile* C, Node* tst) : Node(nullptr, tst) {
init_class_id(Class_OpaqueNotNull);
OpaqueConstantBoolNode(Compile* C, Node* tst, bool constant) : Node(nullptr, tst), _constant(constant) {
assert(tst->is_Bool() || tst->is_Con(), "Test node must be a BoolNode or a constant");
init_class_id(Class_OpaqueConstantBool);
init_flags(Flag_is_macro);
C->add_macro_node(this);
}
@ -146,6 +157,9 @@ class OpaqueNotNullNode : public Node {
virtual int Opcode() const;
virtual const Type* Value(PhaseGVN* phase) const;
virtual const Type* bottom_type() const { return TypeInt::BOOL; }
int constant() const { return _constant ? 1 : 0; }
virtual uint size_of() const { return sizeof(OpaqueConstantBoolNode); }
NOT_PRODUCT(void dump_spec(outputStream* st) const);
};
// This node is used for Template Assertion Predicate BoolNodes. A Template Assertion Predicate is always removed

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2026, 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
@ -307,7 +307,7 @@ bool PhaseIdealLoop::clone_cmp_down(Node* n, const Node* blk1, const Node* blk2)
assert( bol->is_Bool(), "" );
if (bol->outcnt() == 1) {
Node* use = bol->unique_out();
if (use->is_OpaqueNotNull() || use->is_OpaqueTemplateAssertionPredicate() ||
if (use->is_OpaqueConstantBool() || use->is_OpaqueTemplateAssertionPredicate() ||
use->is_OpaqueInitializedAssertionPredicate()) {
if (use->outcnt() == 1) {
Node* iff = use->unique_out();
@ -331,8 +331,8 @@ bool PhaseIdealLoop::clone_cmp_down(Node* n, const Node* blk1, const Node* blk2)
// Recursively sink any BoolNode
for (DUIterator j = bol->outs(); bol->has_out(j); j++) {
Node* u = bol->out(j);
// Uses are either IfNodes, CMoves, OpaqueNotNull, or Opaque*AssertionPredicate
if (u->is_OpaqueNotNull() || u->is_OpaqueTemplateAssertionPredicate() ||
// Uses are either IfNodes, CMoves, OpaqueConstantBool or Opaque*AssertionPredicate
if (u->is_OpaqueConstantBool() || u->is_OpaqueTemplateAssertionPredicate() ||
u->is_OpaqueInitializedAssertionPredicate()) {
assert(u->in(1) == bol, "bad input");
for (DUIterator_Last kmin, k = u->last_outs(kmin); k >= kmin; --k) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2026, 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
@ -1111,7 +1111,7 @@ public final class String
int sp = 0;
int sl = len;
while (sp < sl) {
int ret = StringCoding.implEncodeISOArray(val, sp, dst, dp, len);
int ret = StringCoding.encodeISOArray(val, sp, dst, dp, len);
sp = sp + ret;
dp = dp + ret;
if (ret != len) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2026, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, Alibaba Group Holding Limited. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
@ -26,8 +26,11 @@
package java.lang;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import java.util.function.BiFunction;
/**
* Utility class for string encoding and decoding.
*/
@ -38,7 +41,7 @@ class StringCoding {
/**
* Count the number of leading non-zero ascii chars in the range.
*/
public static int countNonZeroAscii(String s) {
static int countNonZeroAscii(String s) {
byte[] value = s.value();
if (s.isLatin1()) {
return countNonZeroAsciiLatin1(value, 0, value.length);
@ -50,7 +53,7 @@ class StringCoding {
/**
* Count the number of non-zero ascii chars in the range.
*/
public static int countNonZeroAsciiLatin1(byte[] ba, int off, int len) {
private static int countNonZeroAsciiLatin1(byte[] ba, int off, int len) {
int limit = off + len;
for (int i = off; i < limit; i++) {
if (ba[i] <= 0) {
@ -63,7 +66,7 @@ class StringCoding {
/**
* Count the number of leading non-zero ascii chars in the range.
*/
public static int countNonZeroAsciiUTF16(byte[] ba, int off, int strlen) {
private static int countNonZeroAsciiUTF16(byte[] ba, int off, int strlen) {
int limit = off + strlen;
for (int i = off; i < limit; i++) {
char c = StringUTF16.charAt(ba, i);
@ -74,7 +77,7 @@ class StringCoding {
return strlen;
}
public static boolean hasNegatives(byte[] ba, int off, int len) {
static boolean hasNegatives(byte[] ba, int off, int len) {
return countPositives(ba, off, len) != len;
}
@ -85,9 +88,24 @@ class StringCoding {
* bytes in the range. If there are negative bytes, the implementation must return
* a value that is less than or equal to the index of the first negative byte
* in the range.
*
* @param ba a byte array
* @param off the index of the first byte to start reading from
* @param len the total number of bytes to read
* @throws NullPointerException if {@code ba} is null
* @throws ArrayIndexOutOfBoundsException if the provided sub-range is
* {@linkplain Preconditions#checkFromIndexSize(int, int, int, BiFunction) out of bounds}
*/
static int countPositives(byte[] ba, int off, int len) {
Preconditions.checkFromIndexSize(
off, len,
ba.length, // Implicit null check on `ba`
Preconditions.AIOOBE_FORMATTER);
return countPositives0(ba, off, len);
}
@IntrinsicCandidate
public static int countPositives(byte[] ba, int off, int len) {
private static int countPositives0(byte[] ba, int off, int len) {
int limit = off + len;
for (int i = off; i < limit; i++) {
if (ba[i] < 0) {
@ -97,9 +115,37 @@ class StringCoding {
return len;
}
/**
* Encodes as many ISO-8859-1 codepoints as possible from the source byte
* array containing characters encoded in UTF-16, into the destination byte
* array, assuming that the encoding is ISO-8859-1 compatible.
*
* @param sa the source byte array containing characters encoded in UTF-16
* @param sp the index of the <em>character (not byte!)</em> from the source array to start reading from
* @param da the target byte array
* @param dp the index of the target array to start writing to
* @param len the maximum number of <em>characters (not bytes!)</em> to be encoded
* @return the total number of <em>characters (not bytes!)</em> successfully encoded
* @throws NullPointerException if any of the provided arrays is null
*/
static int encodeISOArray(byte[] sa, int sp,
byte[] da, int dp, int len) {
// This method should tolerate invalid arguments, matching the lenient behavior of the VM intrinsic.
// Hence, using operator expressions instead of `Preconditions`, which throw on failure.
int sl;
if ((sp | dp | len) < 0 ||
// Halving the length of `sa` to obtain the number of characters:
sp >= (sl = sa.length >>> 1) || // Implicit null check on `sa`
dp >= da.length) { // Implicit null check on `da`
return 0;
}
int minLen = Math.min(len, Math.min(sl - sp, da.length - dp));
return encodeISOArray0(sa, sp, da, dp, minLen);
}
@IntrinsicCandidate
public static int implEncodeISOArray(byte[] sa, int sp,
byte[] da, int dp, int len) {
private static int encodeISOArray0(byte[] sa, int sp,
byte[] da, int dp, int len) {
int i = 0;
for (; i < len; i++) {
char c = StringUTF16.getChar(sa, sp++);
@ -110,10 +156,35 @@ class StringCoding {
return i;
}
/**
* Encodes as many ASCII codepoints as possible from the source
* character array into the destination byte array, assuming that
* the encoding is ASCII compatible.
*
* @param sa the source character array
* @param sp the index of the source array to start reading from
* @param da the target byte array
* @param dp the index of the target array to start writing to
* @param len the maximum number of characters to be encoded
* @return the total number of characters successfully encoded
* @throws NullPointerException if any of the provided arrays is null
*/
static int encodeAsciiArray(char[] sa, int sp,
byte[] da, int dp, int len) {
// This method should tolerate invalid arguments, matching the lenient behavior of the VM intrinsic.
// Hence, using operator expressions instead of `Preconditions`, which throw on failure.
if ((sp | dp | len) < 0 ||
sp >= sa.length || // Implicit null check on `sa`
dp >= da.length) { // Implicit null check on `da`
return 0;
}
int minLen = Math.min(len, Math.min(sa.length - sp, da.length - dp));
return encodeAsciiArray0(sa, sp, da, dp, minLen);
}
@IntrinsicCandidate
public static int implEncodeAsciiArray(char[] sa, int sp,
byte[] da, int dp, int len)
{
static int encodeAsciiArray0(char[] sa, int sp,
byte[] da, int dp, int len) {
int i = 0;
for (; i < len; i++) {
char c = sa[sp++];

View File

@ -2176,8 +2176,8 @@ public final class System {
return String.decodeASCII(src, srcOff, dst, dstOff, len);
}
public int uncheckedEncodeASCII(char[] src, int srcOff, byte[] dst, int dstOff, int len) {
return StringCoding.implEncodeAsciiArray(src, srcOff, dst, dstOff, len);
public int encodeASCII(char[] sa, int sp, byte[] da, int dp, int len) {
return StringCoding.encodeAsciiArray(sa, sp, da, dp, len);
}
public InputStream initialSystemIn() {

View File

@ -448,15 +448,19 @@ public interface JavaLangAccess {
PrintStream initialSystemErr();
/**
* Encodes as many ASCII codepoints as possible from the source array into
* the destination byte array, assuming that the encoding is ASCII
* compatible.
* <p>
* <b>WARNING: This method does not perform any bound checks.</b>
* Encodes as many ASCII codepoints as possible from the source
* character array into the destination byte array, assuming that
* the encoding is ASCII compatible.
*
* @return the number of bytes successfully encoded, or 0 if none
* @param sa the source character array
* @param sp the index of the source array to start reading from
* @param da the target byte array
* @param dp the index of the target array to start writing to
* @param len the total number of characters to be encoded
* @return the total number of characters successfully encoded
* @throws NullPointerException if any of the provided arrays is null
*/
int uncheckedEncodeASCII(char[] src, int srcOff, byte[] dst, int dstOff, int len);
int encodeASCII(char[] sa, int sp, byte[] da, int dp, int len);
/**
* Set the cause of Throwable

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2026, 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
@ -445,7 +445,7 @@ class CESU_8 extends Unicode
int dl = dst.arrayOffset() + dst.limit();
// Handle ASCII-only prefix
int n = JLA.uncheckedEncodeASCII(sa, sp, da, dp, Math.min(sl - sp, dl - dp));
int n = JLA.encodeASCII(sa, sp, da, dp, Math.min(sl - sp, dl - dp));
sp += n;
dp += n;

View File

@ -610,7 +610,7 @@ public class DoubleByte {
try {
if (isASCIICompatible) {
int n = JLA.uncheckedEncodeASCII(sa, sp, da, dp, Math.min(dl - dp, sl - sp));
int n = JLA.encodeASCII(sa, sp, da, dp, Math.min(dl - dp, sl - sp));
sp += n;
dp += n;
}

View File

@ -35,7 +35,6 @@ import java.util.Objects;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.IntrinsicCandidate;
public class ISO_8859_1
@ -152,20 +151,34 @@ public class ISO_8859_1
private final Surrogate.Parser sgp = new Surrogate.Parser();
// Method possible replaced with a compiler intrinsic.
/**
* Encodes as many ISO-8859-1 codepoints as possible from the source
* character array into the destination byte array, assuming that
* the encoding is ISO-8859-1 compatible.
*
* @param sa the source character array
* @param sp the index of the source array to start reading from
* @param da the target byte array
* @param dp the index of the target array to start writing to
* @param len the maximum number of characters to be encoded
* @return the total number of characters successfully encoded
* @throws NullPointerException if any of the provided arrays is null
*/
private static int encodeISOArray(char[] sa, int sp,
byte[] da, int dp, int len) {
if (len <= 0) {
// This method should tolerate invalid arguments, matching the lenient behavior of the VM intrinsic.
// Hence, using operator expressions instead of `Preconditions`, which throw on failure.
if ((sp | dp | len) < 0 ||
sp >= sa.length || // Implicit null check on `sa`
dp >= da.length) { // Implicit null check on `da`
return 0;
}
encodeISOArrayCheck(sa, sp, da, dp, len);
return implEncodeISOArray(sa, sp, da, dp, len);
int minLen = Math.min(len, Math.min(sa.length - sp, da.length - dp));
return encodeISOArray0(sa, sp, da, dp, minLen);
}
@IntrinsicCandidate
private static int implEncodeISOArray(char[] sa, int sp,
byte[] da, int dp, int len)
{
private static int encodeISOArray0(char[] sa, int sp, byte[] da, int dp, int len) {
int i = 0;
for (; i < len; i++) {
char c = sa[sp++];
@ -176,17 +189,6 @@ public class ISO_8859_1
return i;
}
private static void encodeISOArrayCheck(char[] sa, int sp,
byte[] da, int dp, int len) {
Objects.requireNonNull(sa);
Objects.requireNonNull(da);
Preconditions.checkIndex(sp, sa.length, Preconditions.AIOOBE_FORMATTER);
Preconditions.checkIndex(dp, da.length, Preconditions.AIOOBE_FORMATTER);
Preconditions.checkIndex(sp + len - 1, sa.length, Preconditions.AIOOBE_FORMATTER);
Preconditions.checkIndex(dp + len - 1, da.length, Preconditions.AIOOBE_FORMATTER);
}
private CoderResult encodeArrayLoop(CharBuffer src,
ByteBuffer dst)
{

View File

@ -227,7 +227,7 @@ public class SingleByte
int len = Math.min(dl - dp, sl - sp);
if (isASCIICompatible) {
int n = JLA.uncheckedEncodeASCII(sa, sp, da, dp, len);
int n = JLA.encodeASCII(sa, sp, da, dp, len);
sp += n;
dp += n;
len -= n;

View File

@ -169,7 +169,7 @@ public class US_ASCII
assert (dp <= dl);
dp = (dp <= dl ? dp : dl);
int n = JLA.uncheckedEncodeASCII(sa, sp, da, dp, Math.min(sl - sp, dl - dp));
int n = JLA.encodeASCII(sa, sp, da, dp, Math.min(sl - sp, dl - dp));
sp += n;
dp += n;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2026, 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
@ -452,7 +452,7 @@ public final class UTF_8 extends Unicode {
int dl = dst.arrayOffset() + dst.limit();
// Handle ASCII-only prefix
int n = JLA.uncheckedEncodeASCII(sa, sp, da, dp, Math.min(sl - sp, dl - dp));
int n = JLA.encodeASCII(sa, sp, da, dp, Math.min(sl - sp, dl - dp));
sp += n;
dp += n;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2026, 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
@ -309,7 +309,7 @@ public class EUC_JP
try {
if (enc0201.isASCIICompatible()) {
int n = JLA.uncheckedEncodeASCII(sa, sp, da, dp, Math.min(dl - dp, sl - sp));
int n = JLA.encodeASCII(sa, sp, da, dp, Math.min(dl - dp, sl - sp));
sp += n;
dp += n;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2026, 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
@ -64,7 +64,7 @@ public class TestCanReduceCheckUsersDifferentIfs {
// (6) CastPP(phi1) ends up at IfFalse of OuterStripMinedLoopEnd of loop (L).
// (7) EA tries to reduce phi1(CheckCastPP(B), CheckCastPP(c)) and looks at
// OuterStripMinedLoopEnd and asserts that if it's not an IfNode that it has
// an OpaqueNotNull which obviously is not the case and the assert fails.
// an OpaqueConstantBool which obviously is not the case and the assert fails.
// (5) Found to be false after PhaseIdealLoop before EA and is folded away.
if (y == 76) {
@ -77,7 +77,7 @@ public class TestCanReduceCheckUsersDifferentIfs {
}
// Same as testOuterStripMinedLoopEnd() but we find in (7) a ParsePredicate from the
// removed loop (L) which also does not have an OpaqueNotNull and the assert fails.
// removed loop (L) which also does not have an OpaqueConstantBool and the assert fails.
static void testParsePredicate() {
A a = flag ? new B() : new C();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2026, 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2026, 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2026, 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

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2026, 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 8374582
* @summary Tests the creation and removal of opaque nodes at range checks points in string intrinsics.
* @requires vm.flagless
* @library /test/lib /
* @run driver ${test.main.class}
*/
package compiler.intrinsics.string;
import compiler.lib.ir_framework.*;
public class TestOpaqueConstantBoolNodes {
static byte[] bytes = new byte[42];
public static void main(String[] args) {
TestFramework.runWithFlags(
"-XX:CompileCommand=inline,java.lang.StringCoding::*",
"-XX:CompileCommand=exclude,jdk.internal.util.Preconditions::checkFromIndexSize");
}
@Setup
private static Object[] setup() {
return new Object[] {bytes, 2, 23};
}
@Test
@IR(counts = {IRNode.OPAQUE_CONSTANT_BOOL, "3"}, phase = CompilePhase.AFTER_PARSING)
@IR(failOn = {IRNode.OPAQUE_CONSTANT_BOOL}, phase = CompilePhase.AFTER_MACRO_EXPANSION)
@Arguments(setup = "setup")
private static String test(byte[] bytes, int i, int l) {
return new String(bytes, i , l);
}
}

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) 2026, 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 8374582
* @summary Tests handling of invalid array indices in C2 intrinsic if explicit range check in Java code is not inlined.
* @modules java.base/jdk.internal.access
* @run main/othervm
* -XX:CompileCommand=inline,java.lang.StringCoding::*
* -XX:CompileCommand=exclude,jdk.internal.util.Preconditions::checkFromIndexSize
* ${test.main.class}
*/
package compiler.intrinsics.string;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
public class TestRangeCheck {
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
public static void main(String[] args) {
byte[] bytes = new byte[42];
for (int i = 0; i < 10_000; ++i) {
test(bytes);
}
}
private static int test(byte[] bytes) {
try {
// Calling `StringCoding::countPositives`, which is a "front door"
// to the `StringCoding::countPositives0` intrinsic.
// `countPositives` validates its input using
// `Preconditions::checkFromIndexSize`, which also maps to an
// intrinsic. When `checkFromIndexSize` is not inlined, C2 does not
// know about the explicit range checks, and does not cut off the
// dead code. As a result, an invalid value (e.g., `-1`) can be fed
// as input into the `countPositives0` intrinsic, get replaced
// by TOP, and cause a failure in the matcher.
return JLA.countPositives(bytes, -1, 42);
} catch (Exception e) {
return 0;
}
}
}

View File

@ -3147,6 +3147,11 @@ public class IRNode {
machOnlyNameRegex(REPLICATE_HF_IMM8, "replicateHF_imm8_gt128b");
}
public static final String OPAQUE_CONSTANT_BOOL = PREFIX + "OPAQUE_CONSTANT_BOOL" + POSTFIX;
static {
beforeMatchingNameRegex(OPAQUE_CONSTANT_BOOL, "OpaqueConstantBool");
}
/*
* Utility methods to set up IR_NODE_MAPPINGS.
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2026, 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2026, 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
@ -65,15 +65,15 @@ public class OpaqueAccesses {
// Finish the line after the node type, skips full line, and eats until before the node types
private static final String SKIP = IRNode.MID + IRNode.END + "\\R" + FULL_LINES + "\\s*" + IRNode.START;
private static final String CALL_STATIC_JAVA_AND_THEN_OPAQUE_NOT_NULL = IRNode.START + "CallStaticJava" + SKIP + "OpaqueNotNull" + IRNode.MID + IRNode.END;
private static final String OPAQUE_NOT_NULL_AND_THEN_CALL_STATIC_JAVA = IRNode.START + "OpaqueNotNull" + SKIP + "CallStaticJava" + IRNode.MID + IRNode.END;
/* Having both CallStaticJava and OpaqueNotNull, in any order. We use that in a failOn to make sure we have one
private static final String CALL_STATIC_JAVA_AND_THEN_OPAQUE_CONSTANT_BOOL = IRNode.START + "CallStaticJava" + SKIP + "OpaqueConstantBool" + IRNode.MID + IRNode.END;
private static final String OPAQUE_CONSTANT_BOOL_AND_THEN_CALL_STATIC_JAVA = IRNode.START + "OpaqueConstantBool" + SKIP + "CallStaticJava" + IRNode.MID + IRNode.END;
/* Having both CallStaticJava and OpaqueConstantBool, in any order. We use that in a failOn to make sure we have one
* or the other (or none), but not both.
* The CallStaticJava happens when the call is not intrinsified, and the OpaqueNotNull comes from the intrinsic.
* The CallStaticJava happens when the call is not intrinsified, and the OpaqueConstantBool comes from the intrinsic.
* We don't want a unfinished intrinsic, with the call nevertheless.
*/
private static final String BOTH_CALL_STATIC_JAVA_AND_OPAQUE_NOT_NULL =
"(" + CALL_STATIC_JAVA_AND_THEN_OPAQUE_NOT_NULL + ") | (" + OPAQUE_NOT_NULL_AND_THEN_CALL_STATIC_JAVA + ")";
"(" + CALL_STATIC_JAVA_AND_THEN_OPAQUE_CONSTANT_BOOL + ") | (" + OPAQUE_CONSTANT_BOOL_AND_THEN_CALL_STATIC_JAVA + ")";
@Test