Skip to content

Commit d4adbe3

Browse files
committedAug 13, 2024
8152207: Perform array bound checks while getting a length of bytecode instructions
Reviewed-by: sgehwolf, phh Backport-of: 68c8a74fbe25918ec50711ce10eff65afcc73b93
1 parent 047b08a commit d4adbe3

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed
 

‎hotspot/src/share/vm/interpreter/bytecodes.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,16 @@ class Bytecodes: AllStatic {
394394
static Code non_breakpoint_code_at(const Method* method, address bcp);
395395

396396
// Bytecode attributes
397-
static bool is_defined (int code) { return 0 <= code && code < number_of_codes && flags(code, false) != 0; }
397+
static bool is_valid (int code) { return 0 <= code && code < number_of_codes; }
398+
static bool is_defined (int code) { return is_valid(code) && flags(code, false) != 0; }
398399
static bool wide_is_defined(int code) { return is_defined(code) && flags(code, true) != 0; }
399400
static const char* name (Code code) { check(code); return _name [code]; }
400401
static BasicType result_type (Code code) { check(code); return _result_type [code]; }
401402
static int depth (Code code) { check(code); return _depth [code]; }
402403
// Note: Length functions must return <=0 for invalid bytecodes.
403404
// Calling check(code) in length functions would throw an unwanted assert.
404-
static int length_for (Code code) { /*no check*/ return _lengths [code] & 0xF; }
405-
static int wide_length_for(Code code) { /*no check*/ return _lengths [code] >> 4; }
405+
static int length_for (Code code) { return is_valid(code) ? _lengths[code] & 0xF : -1; }
406+
static int wide_length_for(Code code) { return is_valid(code) ? _lengths[code] >> 4 : -1; }
406407
static bool can_trap (Code code) { check(code); return has_all_flags(code, _bc_can_trap, false); }
407408
static Code java_code (Code code) { check(code); return _java_code [code]; }
408409
static bool can_rewrite (Code code) { check(code); return has_all_flags(code, _bc_can_rewrite, false); }

‎jdk/src/share/native/common/check_code.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -1731,9 +1731,14 @@ static int instruction_length(unsigned char *iptr, unsigned char *end)
17311731
}
17321732

17331733
default: {
1734+
if (instruction < 0 || instruction > JVM_OPC_MAX)
1735+
return -1;
1736+
17341737
/* A length of 0 indicates an error. */
1735-
int length = opcode_length[instruction];
1736-
return (length <= 0) ? -1 : length;
1738+
if (opcode_length[instruction] <= 0)
1739+
return -1;
1740+
1741+
return opcode_length[instruction];
17371742
}
17381743
}
17391744
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Aug 13, 2024

@openjdk-notifier[bot]
Please sign in to comment.