From d344a67daff3509600fcdc2850856314c592ef1d Mon Sep 17 00:00:00 2001 From: Andrew Dinn Date: Tue, 19 Nov 2024 14:58:35 +0000 Subject: [PATCH] Verify stubs are generated in correct blob --- .../cpu/aarch64/stubGenerator_aarch64.cpp | 2 +- src/hotspot/cpu/arm/stubGenerator_arm.cpp | 2 +- src/hotspot/cpu/ppc/stubGenerator_ppc.cpp | 2 +- src/hotspot/cpu/riscv/stubGenerator_riscv.cpp | 2 +- src/hotspot/cpu/s390/stubGenerator_s390.cpp | 2 +- src/hotspot/cpu/x86/stubGenerator_x86_32.cpp | 2 +- src/hotspot/cpu/x86/stubGenerator_x86_64.cpp | 2 +- src/hotspot/cpu/zero/stubGenerator_zero.cpp | 2 +- .../share/runtime/stubCodeGenerator.cpp | 18 +++++++++++++++++- .../share/runtime/stubCodeGenerator.hpp | 17 ++++++++++++----- src/hotspot/share/runtime/stubRoutines.cpp | 6 +++--- src/hotspot/share/runtime/stubRoutines.hpp | 5 +++++ 12 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/hotspot/cpu/aarch64/stubGenerator_aarch64.cpp b/src/hotspot/cpu/aarch64/stubGenerator_aarch64.cpp index 762c779ab0137..f1737bb4bad10 100644 --- a/src/hotspot/cpu/aarch64/stubGenerator_aarch64.cpp +++ b/src/hotspot/cpu/aarch64/stubGenerator_aarch64.cpp @@ -8931,7 +8931,7 @@ class StubGenerator: public StubCodeGenerator { } public: - StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code) { + StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code, blob_id) { switch(blob_id) { case initial_id: generate_initial_stubs(); diff --git a/src/hotspot/cpu/arm/stubGenerator_arm.cpp b/src/hotspot/cpu/arm/stubGenerator_arm.cpp index 1dcb7b5f8881d..8e38fb4c79350 100644 --- a/src/hotspot/cpu/arm/stubGenerator_arm.cpp +++ b/src/hotspot/cpu/arm/stubGenerator_arm.cpp @@ -3204,7 +3204,7 @@ class StubGenerator: public StubCodeGenerator { } public: - StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code) { + StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code, blob_id) { switch(blob_id) { case initial_id: generate_initial_stubs(); diff --git a/src/hotspot/cpu/ppc/stubGenerator_ppc.cpp b/src/hotspot/cpu/ppc/stubGenerator_ppc.cpp index f08a0a7886c9f..233235a591ea5 100644 --- a/src/hotspot/cpu/ppc/stubGenerator_ppc.cpp +++ b/src/hotspot/cpu/ppc/stubGenerator_ppc.cpp @@ -5023,7 +5023,7 @@ address generate_lookup_secondary_supers_table_stub(u1 super_klass_index) { } public: - StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code) { + StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code, blob_id) { switch(blob_id) { case initial_id: generate_initial_stubs(); diff --git a/src/hotspot/cpu/riscv/stubGenerator_riscv.cpp b/src/hotspot/cpu/riscv/stubGenerator_riscv.cpp index 2c0e83d4ac02b..4ed50bd9e7a6c 100644 --- a/src/hotspot/cpu/riscv/stubGenerator_riscv.cpp +++ b/src/hotspot/cpu/riscv/stubGenerator_riscv.cpp @@ -6610,7 +6610,7 @@ static const int64_t right_3_bits = right_n_bits(3); } public: - StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code) { + StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code, blob_id) { switch(blob_id) { case initial_id: generate_initial_stubs(); diff --git a/src/hotspot/cpu/s390/stubGenerator_s390.cpp b/src/hotspot/cpu/s390/stubGenerator_s390.cpp index 4fe56f9aa174a..0570ac044ee93 100644 --- a/src/hotspot/cpu/s390/stubGenerator_s390.cpp +++ b/src/hotspot/cpu/s390/stubGenerator_s390.cpp @@ -3300,7 +3300,7 @@ class StubGenerator: public StubCodeGenerator { } public: - StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code) { + StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code, blob_id) { switch(blob_id) { case initial_id: generate_initial_stubs(); diff --git a/src/hotspot/cpu/x86/stubGenerator_x86_32.cpp b/src/hotspot/cpu/x86/stubGenerator_x86_32.cpp index 9a989965a0a0b..6dcb6992662ce 100644 --- a/src/hotspot/cpu/x86/stubGenerator_x86_32.cpp +++ b/src/hotspot/cpu/x86/stubGenerator_x86_32.cpp @@ -4300,7 +4300,7 @@ class StubGenerator: public StubCodeGenerator { public: - StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code) { + StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code, blob_id) { switch(blob_id) { case initial_id: generate_initial_stubs(); diff --git a/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp b/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp index aa3bdcd422beb..b6c44d524055d 100644 --- a/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp +++ b/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp @@ -4412,7 +4412,7 @@ void StubGenerator::generate_compiler_stubs() { #endif // COMPILER2_OR_JVMCI } -StubGenerator::StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code) { +StubGenerator::StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code, blob_id) { switch(blob_id) { case initial_id: generate_initial_stubs(); diff --git a/src/hotspot/cpu/zero/stubGenerator_zero.cpp b/src/hotspot/cpu/zero/stubGenerator_zero.cpp index 99174c28f57c0..5b764e6977fb2 100644 --- a/src/hotspot/cpu/zero/stubGenerator_zero.cpp +++ b/src/hotspot/cpu/zero/stubGenerator_zero.cpp @@ -213,7 +213,7 @@ class StubGenerator: public StubCodeGenerator { } public: - StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code) { + StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code, blob_id) { switch(blob_id) { case initial_id: generate_initial_stubs(); diff --git a/src/hotspot/share/runtime/stubCodeGenerator.cpp b/src/hotspot/share/runtime/stubCodeGenerator.cpp index 4c14864be81bd..22025b32fac32 100644 --- a/src/hotspot/share/runtime/stubCodeGenerator.cpp +++ b/src/hotspot/share/runtime/stubCodeGenerator.cpp @@ -70,6 +70,13 @@ void StubCodeDesc::print() const { print_on(tty); } StubCodeGenerator::StubCodeGenerator(CodeBuffer* code, bool print_code) { _masm = new MacroAssembler(code); + _blob_id = StubGenBlobId::NO_BLOBID; + _print_code = PrintStubCode || print_code; +} + +StubCodeGenerator::StubCodeGenerator(CodeBuffer* code, StubGenBlobId blob_id, bool print_code) { + _masm = new MacroAssembler(code); + _blob_id = blob_id; _print_code = PrintStubCode || print_code; } @@ -112,6 +119,11 @@ void StubCodeGenerator::stub_epilog(StubCodeDesc* cdesc) { } } +#ifndef PRODUCT +void StubCodeGenerator::verify_stub(StubGenStubId stub_id) { + assert(StubRoutines::stub_to_blob(stub_id) == blob_id(), "wrong blob being used to generate stub"); +} +#endif // Implementation of CodeMark @@ -123,7 +135,11 @@ StubCodeMark::StubCodeMark(StubCodeGenerator* cgen, const char* group, const cha _cdesc->set_begin(_cgen->assembler()->pc()); } -StubCodeMark::StubCodeMark(StubCodeGenerator* cgen, StubGenStubId stub_id) : StubCodeMark(cgen, "StubRoutines", StubRoutines::get_stub_name(stub_id)) { } +StubCodeMark::StubCodeMark(StubCodeGenerator* cgen, StubGenStubId stub_id) : StubCodeMark(cgen, "StubRoutines", StubRoutines::get_stub_name(stub_id)) { +#ifndef PRODUCT + cgen->verify_stub(stub_id); +#endif +} StubCodeMark::~StubCodeMark() { _cgen->assembler()->flush(); diff --git a/src/hotspot/share/runtime/stubCodeGenerator.hpp b/src/hotspot/share/runtime/stubCodeGenerator.hpp index f3266bd7b0454..4bd2916a862fb 100644 --- a/src/hotspot/share/runtime/stubCodeGenerator.hpp +++ b/src/hotspot/share/runtime/stubCodeGenerator.hpp @@ -96,29 +96,36 @@ class StubCodeDesc: public CHeapObj { void print() const; }; +// forward declare blob and stub id enums + +enum StubGenBlobId : int; +enum StubGenStubId : int; + // The base class for all stub-generating code generators. // Provides utility functions. class StubCodeGenerator: public StackObj { private: bool _print_code; - + StubGenBlobId _blob_id; protected: MacroAssembler* _masm; public: StubCodeGenerator(CodeBuffer* code, bool print_code = false); + StubCodeGenerator(CodeBuffer* code, StubGenBlobId blob_id, bool print_code = false); ~StubCodeGenerator(); MacroAssembler* assembler() const { return _masm; } + StubGenBlobId blob_id() { return _blob_id; } virtual void stub_prolog(StubCodeDesc* cdesc); // called by StubCodeMark constructor virtual void stub_epilog(StubCodeDesc* cdesc); // called by StubCodeMark destructor -}; - -// forward declare stub id enum -enum StubGenStubId : int; +#ifndef PRODUCT + void verify_stub(StubGenStubId stub_id); +#endif +}; // Stack-allocated helper class used to associate a stub code with a name. // All stub code generating functions that use a StubCodeMark will be registered diff --git a/src/hotspot/share/runtime/stubRoutines.cpp b/src/hotspot/share/runtime/stubRoutines.cpp index de26cd95bcf12..1ea4fe80e1a48 100644 --- a/src/hotspot/share/runtime/stubRoutines.cpp +++ b/src/hotspot/share/runtime/stubRoutines.cpp @@ -221,13 +221,13 @@ bool _verified_stub_ids = verifyStubIds(); // translate a global stub id to an associated blob id based on the // computed blob limits -StubGenBlobId stub_to_blob(StubGenStubId stubId) { +StubGenBlobId StubRoutines::stub_to_blob(StubGenStubId stubId) { int id = (int)stubId; - assert(id > ((int)StubGenStubId::NO_STUBID) && id < ((int)StubGenStubId::NUM_STUBIDS), "stub id out of range"); + assert(id > ((int)StubGenStubId::NO_STUBID) && id < ((int)StubGenStubId::NUM_STUBIDS), "stub id out of range!"); // start with no blob to catch stub id == -1 StubGenBlobId blobId = StubGenBlobId::NO_BLOBID; STUBGEN_BLOBS_DO(BLOB_CHECK_OFFSET); - // if we reach here we should have the lats blob id + // if we reach here we should have the last blob id assert(blobId == StubGenBlobId::NUM_BLOBIDS - 1, "unexpected blob id"); return blobId; } diff --git a/src/hotspot/share/runtime/stubRoutines.hpp b/src/hotspot/share/runtime/stubRoutines.hpp index 47f994a97a1f2..f558a8c56faf8 100644 --- a/src/hotspot/share/runtime/stubRoutines.hpp +++ b/src/hotspot/share/runtime/stubRoutines.hpp @@ -329,6 +329,11 @@ class StubRoutines: AllStatic { #undef DEFINE_BLOB_GETTER +#ifndef PRODUCT + // provide a translation from stub id to its associated blob id + static StubGenBlobId stub_to_blob(StubGenStubId stubId); +#endif + // Debugging static jint verify_oop_count() { return _verify_oop_count; } static jint* verify_oop_count_addr() { return &_verify_oop_count; }