Skip to content

Commit

Permalink
8313564: Fix -Wconversion warnings in classfile code
Browse files Browse the repository at this point in the history
Reviewed-by: matsaave, dholmes
  • Loading branch information
coleenp committed Aug 4, 2023
1 parent e8a37b9 commit f66cd50
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/hotspot/cpu/x86/vm_version_x86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,7 @@ void VM_Version::get_processor_features() {
}

// Allocation prefetch settings
intx cache_line_size = prefetch_data_size();
int cache_line_size = checked_cast<int>(prefetch_data_size());
if (FLAG_IS_DEFAULT(AllocatePrefetchStepSize) &&
(cache_line_size > AllocatePrefetchStepSize)) {
FLAG_SET_DEFAULT(AllocatePrefetchStepSize, cache_line_size);
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/classfile/altHashing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static void halfsiphash_adddata(uint32_t v[4], uint32_t newdata, int rounds) {

static void halfsiphash_init32(uint32_t v[4], uint64_t seed) {
v[0] = seed & 0xffffffff;
v[1] = seed >> 32;
v[1] = (uint32_t)(seed >> 32);
v[2] = 0x6c796765 ^ v[0];
v[3] = 0x74656462 ^ v[1];
}
Expand Down
28 changes: 14 additions & 14 deletions src/hotspot/share/classfile/classFileParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,12 +965,12 @@ class AnnotationCollector : public ResourceObj{
// Set the annotation name:
void set_annotation(ID id) {
assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
_annotations_present |= nth_bit((int)id);
_annotations_present |= (int)nth_bit((int)id);
}

void remove_annotation(ID id) {
assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
_annotations_present &= ~nth_bit((int)id);
_annotations_present &= (int)~nth_bit((int)id);
}

// Report if the annotation is present.
Expand Down Expand Up @@ -1737,8 +1737,8 @@ const ClassFileParser::unsafe_u2* ClassFileParser::parse_localvariable_table(con
TRAPS) {
const char* const tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable";
*localvariable_table_length = cfs->get_u2(CHECK_NULL);
const unsigned int size =
(*localvariable_table_length) * sizeof(Classfile_LVT_Element) / sizeof(u2);
const unsigned int size = checked_cast<unsigned>(
(*localvariable_table_length) * sizeof(Classfile_LVT_Element) / sizeof(u2));

const ConstantPool* const cp = _cp;

Expand Down Expand Up @@ -2349,23 +2349,23 @@ Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,

calculated_attribute_length =
sizeof(max_stack) + sizeof(max_locals) + sizeof(code_length);
calculated_attribute_length +=
calculated_attribute_length += checked_cast<unsigned int>(
code_length +
sizeof(exception_table_length) +
sizeof(code_attributes_count) +
exception_table_length *
( sizeof(u2) + // start_pc
sizeof(u2) + // end_pc
sizeof(u2) + // handler_pc
sizeof(u2) ); // catch_type_index
sizeof(u2) )); // catch_type_index

while (code_attributes_count--) {
cfs->guarantee_more(6, CHECK_NULL); // code_attribute_name_index, code_attribute_length
const u2 code_attribute_name_index = cfs->get_u2_fast();
const u4 code_attribute_length = cfs->get_u4_fast();
calculated_attribute_length += code_attribute_length +
sizeof(code_attribute_name_index) +
sizeof(code_attribute_length);
(unsigned)sizeof(code_attribute_name_index) +
(unsigned)sizeof(code_attribute_length);
check_property(valid_symbol_at(code_attribute_name_index),
"Invalid code attribute name index %u in class file %s",
code_attribute_name_index,
Expand Down Expand Up @@ -2479,7 +2479,7 @@ Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
}
method_parameters_seen = true;
method_parameters_length = cfs->get_u1_fast();
const u2 real_length = (method_parameters_length * 4u) + 1u;
const u4 real_length = (method_parameters_length * 4u) + 1u;
if (method_attribute_length != real_length) {
classfile_parse_error(
"Invalid MethodParameters method attribute length %u in class file",
Expand Down Expand Up @@ -3202,7 +3202,7 @@ u2 ClassFileParser::parse_classfile_permitted_subclasses_attribute(const ClassFi
// u2 attributes_count;
// attribute_info_attributes[attributes_count];
// }
u2 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
u4 ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
const ConstantPool* cp,
const u1* const record_attribute_start,
TRAPS) {
Expand Down Expand Up @@ -3404,7 +3404,7 @@ void ClassFileParser::parse_classfile_bootstrap_methods_attribute(const ClassFil
// The attribute contains a counted array of counted tuples of shorts,
// represending bootstrap specifiers:
// length*{bootstrap_method_index, argument_count*{argument_index}}
const int operand_count = (attribute_byte_length - sizeof(u2)) / sizeof(u2);
const unsigned int operand_count = (attribute_byte_length - (unsigned)sizeof(u2)) / (unsigned)sizeof(u2);
// operand_count = number of shorts in attr, except for leading length

// The attribute is copied into a short[] array.
Expand Down Expand Up @@ -4812,7 +4812,7 @@ const char* ClassFileParser::skip_over_field_signature(const char* signature,
const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1);
// Format check signature
if (c != nullptr) {
int newlen = c - (char*) signature;
int newlen = pointer_delta_as_int(c, (char*) signature);
bool legal = verify_unqualified_name(signature, newlen, LegalClass);
if (!legal) {
classfile_parse_error("Class name is empty or contains illegal character "
Expand Down Expand Up @@ -5022,7 +5022,7 @@ int ClassFileParser::verify_legal_method_signature(const Symbol* name,
if (p[0] == 'J' || p[0] == 'D') {
args_size++;
}
length -= nextp - p;
length -= pointer_delta_as_int(nextp, p);
p = nextp;
nextp = skip_over_field_signature(p, false, length, CHECK_0);
}
Expand Down Expand Up @@ -5241,7 +5241,7 @@ void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
// size is equal to the number of methods in the class. If
// that changes, then InstanceKlass::idnum_can_increment()
// has to be changed accordingly.
ik->set_initial_method_idnum(ik->methods()->length());
ik->set_initial_method_idnum(checked_cast<u2>(ik->methods()->length()));

ik->set_this_class_index(_this_class_index);

Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/classfile/classFileParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ class ClassFileParser {
const u1* const permitted_subclasses_attribute_start,
TRAPS);

u2 parse_classfile_record_attribute(const ClassFileStream* const cfs,
u4 parse_classfile_record_attribute(const ClassFileStream* const cfs,
const ConstantPool* cp,
const u1* const record_attribute_start,
TRAPS);
Expand Down
8 changes: 5 additions & 3 deletions src/hotspot/share/classfile/classLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ Symbol* ClassLoader::package_from_class_name(const Symbol* name, bool* bad_class
}
return nullptr;
}
return SymbolTable::new_symbol(name, start - base, end - base);
return SymbolTable::new_symbol(name, pointer_delta_as_int(start, base), pointer_delta_as_int(end, base));
}

// Given a fully qualified package name, find its defining package in the class loader's
Expand Down Expand Up @@ -269,9 +269,11 @@ ClassFileStream* ClassPathDirEntry::open_stream(JavaThread* current, const char*
// debug builds so that we guard against use-after-free bugs.
FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
#endif
// We don't verify the length of the classfile stream fits in an int, but this is the
// bootloader so we have control of this.
// Resource allocated
return new ClassFileStream(buffer,
st.st_size,
checked_cast<int>(st.st_size),
_dir,
ClassFileStream::verify);
}
Expand Down Expand Up @@ -420,7 +422,7 @@ ClassFileStream* ClassPathImageEntry::open_stream_for_loader(JavaThread* current
// Resource allocated
assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be");
return new ClassFileStream((u1*)data,
(int)size,
checked_cast<int>(size),
_name,
ClassFileStream::verify,
true); // from_boot_loader_modules_image
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/classfile/classLoaderExt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void ClassLoaderExt::process_jar_manifest(JavaThread* current, ClassPathEntry* e
if (dir_tail == nullptr) {
dir_len = 0;
} else {
dir_len = dir_tail - dir_name + 1;
dir_len = pointer_delta_as_int(dir_tail, dir_name) + 1;
}

// Split the cp_attr by spaces, and add each file
Expand Down
6 changes: 3 additions & 3 deletions src/hotspot/share/classfile/compactHashtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ int HashtableTextDump::scan_symbol_prefix() {
return utf8_length;
}

jchar HashtableTextDump::unescape(const char* from, const char* end, int count) {
jchar value = 0;
int HashtableTextDump::unescape(const char* from, const char* end, int count) {
int value = 0;

corrupted_if(from + count > end, "Truncated");

Expand Down Expand Up @@ -409,7 +409,7 @@ void HashtableTextDump::get_utf8(char* utf8_buffer, int utf8_length) {
switch (c) {
case 'x':
{
jchar value = unescape(from, end, 2);
int value = unescape(from, end, 2);
from += 2;
assert(value <= 0xff, "sanity");
*to++ = (char)(value & 0xff);
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/classfile/compactHashtable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ class HashtableTextDump {
int scan_string_prefix();
int scan_symbol_prefix();

jchar unescape(const char* from, const char* end, int count);
int unescape(const char* from, const char* end, int count);
void get_utf8(char* utf8_buffer, int utf8_length);
static void put_utf8(outputStream* st, const char* utf8_string, int utf8_length);
};
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/classfile/klassFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ InstanceKlass* KlassFactory::check_shared_class_file_load_hook(
ClassLoaderData::class_loader_data(class_loader());
s2 path_index = ik->shared_classpath_index();
ClassFileStream* stream = new ClassFileStream(ptr,
end_ptr - ptr,
pointer_delta_as_int(end_ptr, ptr),
cfs->source(),
ClassFileStream::verify);
ClassLoadInfo cl_info(protection_domain);
Expand Down Expand Up @@ -155,7 +155,7 @@ static ClassFileStream* check_class_file_load_hook(ClassFileStream* stream,
// JVMTI agent has modified class file data.
// Set new class file stream using JVMTI agent modified class file data.
stream = new ClassFileStream(ptr,
end_ptr - ptr,
pointer_delta_as_int(end_ptr, ptr),
stream->source(),
stream->need_verify());
}
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/classfile/loaderConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ void LoaderConstraintTable::print_table_statistics(outputStream* st) {
int len = set.num_constraints();
for (int i = 0; i < len; i++) {
LoaderConstraint* probe = set.constraint_at(i);
sum += sizeof(*probe) + (probe->num_loaders() * sizeof(ClassLoaderData*));
sum += (int)(sizeof(*probe) + (probe->num_loaders() * sizeof(ClassLoaderData*)));
}
return sum;
};
Expand Down
14 changes: 7 additions & 7 deletions src/hotspot/share/classfile/stringTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ volatile bool _alt_hash = false;
static bool _rehashed = false;
static uint64_t _alt_hash_seed = 0;

uintx hash_string(const jchar* s, int len, bool useAlt) {
unsigned int hash_string(const jchar* s, int len, bool useAlt) {
return useAlt ?
AltHashing::halfsiphash_32(_alt_hash_seed, s, len) :
java_lang_String::hash_code(s, len);
Expand Down Expand Up @@ -241,12 +241,12 @@ void StringTable::create_table() {
#endif
}

size_t StringTable::item_added() {
return Atomic::add(&_items_count, (size_t)1);
void StringTable::item_added() {
Atomic::inc(&_items_count);
}

void StringTable::item_removed() {
Atomic::add(&_items_count, (size_t)-1);
Atomic::dec(&_items_count);
}

double StringTable::get_load_factor() {
Expand Down Expand Up @@ -802,7 +802,7 @@ oop StringTable::lookup_shared(const jchar* name, int len) {
void StringTable::allocate_shared_strings_array(TRAPS) {
assert(DumpSharedSpaces, "must be");
if (_items_count > (size_t)max_jint) {
fatal("Too many strings to be archived: " SIZE_FORMAT, _items_count);
fatal("Too many strings to be archived: %zu", _items_count);
}

int total = (int)_items_count;
Expand All @@ -825,7 +825,7 @@ void StringTable::allocate_shared_strings_array(TRAPS) {
// This can only happen if you have an extremely large number of classes that
// refer to more than 16384 * 16384 = 26M interned strings! Not a practical concern
// but bail out for safety.
log_error(cds)("Too many strings to be archived: " SIZE_FORMAT, _items_count);
log_error(cds)("Too many strings to be archived: %zu", _items_count);
MetaspaceShared::unrecoverable_writing_error();
}

Expand Down Expand Up @@ -888,7 +888,7 @@ oop StringTable::init_shared_table(const DumpedInternedStrings* dumped_interned_
verify_secondary_array_index_bits();

_shared_table.reset();
CompactHashtableWriter writer(_items_count, ArchiveBuilder::string_stats());
CompactHashtableWriter writer((int)_items_count, ArchiveBuilder::string_stats());

int index = 0;
auto copy_into_array = [&] (oop string, bool value_ignored) {
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/classfile/stringTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class StringTable : public CHeapObj<mtSymbol>{
static void gc_notification(size_t num_dead);
static void trigger_concurrent_work();

static size_t item_added();
static void item_added();
static void item_removed();

static oop intern(Handle string_or_null_h, const jchar* name, int len, TRAPS);
Expand Down
13 changes: 8 additions & 5 deletions src/hotspot/share/classfile/symbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ static inline void log_trace_symboltable_helper(Symbol* sym, const char* msg) {
}

// Pick hashing algorithm.
static uintx hash_symbol(const char* s, int len, bool useAlt) {
static unsigned int hash_symbol(const char* s, int len, bool useAlt) {
return useAlt ?
AltHashing::halfsiphash_32(_alt_hash_seed, (const uint8_t*)s, len) :
java_lang_String::hash_code((const jbyte*)s, len);
}

#if INCLUDE_CDS
static uintx hash_shared_symbol(const char* s, int len) {
static unsigned int hash_shared_symbol(const char* s, int len) {
return java_lang_String::hash_code((const jbyte*)s, len);
}
#endif
Expand Down Expand Up @@ -237,7 +237,7 @@ void SymbolTable::item_removed() {
}

double SymbolTable::get_load_factor() {
return (double)_items_count/_current_size;
return (double)_items_count/(double)_current_size;
}

size_t SymbolTable::table_size() {
Expand Down Expand Up @@ -657,6 +657,9 @@ void SymbolTable::copy_shared_symbol_table(GrowableArray<Symbol*>* symbols,
}

size_t SymbolTable::estimate_size_for_archive() {
if (_items_count > (size_t)max_jint) {
fatal("Too many symbols to be archived: %zu", _items_count);
}
return CompactHashtableWriter::estimate_size(int(_items_count));
}

Expand Down Expand Up @@ -923,14 +926,14 @@ void SymbolTable::print_histogram() {
tty->print_cr(" Total removed " SIZE_FORMAT_W(7), _symbols_removed);
if (_symbols_counted > 0) {
tty->print_cr(" Percent removed %3.2f",
((float)_symbols_removed / _symbols_counted) * 100);
((double)_symbols_removed / (double)_symbols_counted) * 100);
}
tty->print_cr(" Reference counts " SIZE_FORMAT_W(7), Symbol::_total_count);
tty->print_cr(" Symbol arena used " SIZE_FORMAT_W(7) "K", arena()->used() / K);
tty->print_cr(" Symbol arena size " SIZE_FORMAT_W(7) "K", arena()->size_in_bytes() / K);
tty->print_cr(" Total symbol length " SIZE_FORMAT_W(7), hi.total_length);
tty->print_cr(" Maximum symbol length " SIZE_FORMAT_W(7), hi.max_length);
tty->print_cr(" Average symbol length %7.2f", ((float)hi.total_length / hi.total_count));
tty->print_cr(" Average symbol length %7.2f", ((double)hi.total_length / (double)hi.total_count));
tty->print_cr(" Symbol length histogram:");
tty->print_cr(" %6s %10s %10s", "Length", "#Symbols", "Size");
for (size_t i = 0; i < hi.results_length; i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/classfile/systemDictionaryShared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,9 @@ class UnregisteredClassesDuplicationChecker : StackObj {
ClassLoaderData* loader_b = b[0]->class_loader_data();

if (loader_a != loader_b) {
return intx(loader_a) - intx(loader_b);
return checked_cast<int>(intptr_t(loader_a) - intptr_t(loader_b));
} else {
return intx(a[0]) - intx(b[0]);
return checked_cast<int>(intptr_t(a[0]) - intptr_t(b[0]));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/hotspot/share/classfile/vmIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,21 +806,21 @@ vmSymbolID vmIntrinsics::class_for(vmIntrinsics::ID id) {
jlong info = intrinsic_info(id);
int shift = 2*vmSymbols::log2_SID_LIMIT + log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 1021, "");
return vmSymbols::as_SID( (info >> shift) & mask );
return vmSymbols::as_SID( checked_cast<int>((info >> shift) & mask));
}

vmSymbolID vmIntrinsics::name_for(vmIntrinsics::ID id) {
jlong info = intrinsic_info(id);
int shift = vmSymbols::log2_SID_LIMIT + log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 1022, "");
return vmSymbols::as_SID( (info >> shift) & mask );
return vmSymbols::as_SID( checked_cast<int>((info >> shift) & mask));
}

vmSymbolID vmIntrinsics::signature_for(vmIntrinsics::ID id) {
jlong info = intrinsic_info(id);
int shift = log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 1023, "");
return vmSymbols::as_SID( (info >> shift) & mask );
return vmSymbols::as_SID( checked_cast<int>((info >> shift) & mask));
}

vmIntrinsics::Flags vmIntrinsics::flags_for(vmIntrinsics::ID id) {
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/jvmci/jvmciCompilerToVMInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ JVMCIObjectArray CompilerToVM::initialize_intrinsics(JVMCI_TRAPS) {
do_bool_flag(CITimeEach) \
do_uintx_flag(CodeCacheSegmentSize) \
do_intx_flag(CodeEntryAlignment) \
do_intx_flag(ContendedPaddingWidth) \
do_int_flag(ContendedPaddingWidth) \
do_bool_flag(DontCompileHugeMethods) \
do_bool_flag(EagerJVMCI) \
do_bool_flag(EnableContended) \
Expand Down

1 comment on commit f66cd50

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.