Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8346836: C2: Introduce a way to verify the correctness of ConstraintCastNodes at runtime #22880

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/hotspot/cpu/x86/c2_MacroAssembler_x86.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, 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 @@ -837,6 +837,42 @@ void C2_MacroAssembler::fast_unlock_lightweight(Register obj, Register reg_rax,
// C2 uses the value of ZF to determine the continuation.
}

#ifdef ASSERT
void C2_MacroAssembler::checked_cast_int(const TypeInt* type, Register dst) {
Label fail;
Label succeed;
cmpl(dst, type->_lo);
jccb(Assembler::less, fail);
cmpl(dst, type->_hi);
jccb(Assembler::lessEqual, succeed);
bind(fail);
stop("Invalid CastII");
bind(succeed);
}

void C2_MacroAssembler::checked_cast_long(const TypeLong* type, Register dst, Register tmp) {
Label fail;
Label succeed;
if (is_simm32(type->_lo)) {
cmpq(dst, checked_cast<int>(type->_lo));
} else {
mov64(tmp, type->_lo);
cmpq(dst, tmp);
}
jccb(Assembler::less, fail);
if (is_simm32(type->_hi)) {
cmpq(dst, checked_cast<int>(type->_hi));
} else {
mov64(tmp, type->_hi);
cmpq(dst, tmp);
}
jccb(Assembler::lessEqual, succeed);
bind(fail);
stop("Invalid CastLL");
bind(succeed);
}
#endif // ASSERT

//-------------------------------------------------------------------------------------------
// Generic instructions support for use in .ad files C2 code generation

Expand Down
7 changes: 6 additions & 1 deletion src/hotspot/cpu/x86/c2_MacroAssembler_x86.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, 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,6 +44,11 @@
Register t, Register thread);
void fast_unlock_lightweight(Register obj, Register reg_rax, Register t, Register thread);

#ifdef ASSERT
void checked_cast_int(const TypeInt* type, Register dst);
void checked_cast_long(const TypeLong* type, Register dst, Register tmp);
#endif

// Generic instructions support for use in .ad files C2 code generation
void vabsnegd(int opcode, XMMRegister dst, XMMRegister src);
void vabsnegd(int opcode, XMMRegister dst, XMMRegister src, int vector_len);
Expand Down
30 changes: 29 additions & 1 deletion src/hotspot/cpu/x86/x86_64.ad
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
// Copyright (c) 2003, 2025, 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 @@ -6991,6 +6991,7 @@ instruct castPP(rRegP dst)

instruct castII(rRegI dst)
%{
predicate(!VerifyConstraintCasts);
match(Set dst (CastII dst));

size(0);
Expand All @@ -7000,8 +7001,22 @@ instruct castII(rRegI dst)
ins_pipe(empty);
%}

instruct castII_checked(rRegI dst, rFlagsReg cr)
%{
predicate(VerifyConstraintCasts);
match(Set dst (CastII dst));

effect(KILL cr);
format %{ "# checked_cast_II $dst" %}
ins_encode %{
DEBUG_ONLY(__ checked_cast_int(bottom_type()->is_int(), $dst$$Register));
%}
ins_pipe(pipe_slow);
%}

instruct castLL(rRegL dst)
%{
predicate(!VerifyConstraintCasts);
match(Set dst (CastLL dst));

size(0);
Expand All @@ -7011,6 +7026,19 @@ instruct castLL(rRegL dst)
ins_pipe(empty);
%}

instruct castLL_checked(rRegL dst, rRegL tmp, rFlagsReg cr)
%{
predicate(VerifyConstraintCasts);
match(Set dst (CastLL dst));

effect(KILL cr, TEMP tmp);
format %{ "# checked_cast_LL $dst" %}
ins_encode %{
DEBUG_ONLY(__ checked_cast_long(bottom_type()->is_long(), $dst$$Register, $tmp$$Register));
%}
ins_pipe(pipe_slow);
%}

instruct castFF(regF dst)
%{
match(Set dst (CastFF dst));
Expand Down
4 changes: 3 additions & 1 deletion src/hotspot/share/opto/c2_MacroAssembler.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2025, 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 @@ -31,6 +31,8 @@
#include "utilities/macros.hpp"

class C2EntryBarrierStub;
class TypeInt;
class TypeLong;

class C2_MacroAssembler: public MacroAssembler {
public:
Expand Down
6 changes: 5 additions & 1 deletion src/hotspot/share/opto/c2_globals.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2025, 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 @@ -660,6 +660,10 @@
develop(bool, VerifyAliases, false, \
"perform extra checks on the results of alias analysis") \
\
develop(bool, VerifyConstraintCasts, false, \
"perform runtime check to verify the value of a " \
"ConstraintCast lies inside its type") \
\
product(intx, MaxInlineLevel, 15, \
"maximum number of nested calls that are inlined by high tier " \
"compiler") \
Expand Down
8 changes: 7 additions & 1 deletion src/hotspot/share/opto/castnode.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2025, 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 @@ -506,6 +506,12 @@ const Type* ConstraintCastNode::widen_type(const PhaseGVN* phase, const Type* re
if (!phase->C->post_loop_opts_phase()) {
return res;
}

// Keep these casts for verification
if (VerifyConstraintCasts) {
return res;
}

const TypeInteger* this_type = res->is_integer(bt);
const TypeInteger* in_type = phase->type(in(1))->isa_integer(bt);
if (in_type != nullptr &&
Expand Down