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

Cleanup type conversions in java_bytecode_parsert::read #8584

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
44 changes: 27 additions & 17 deletions jbmc/src/java_bytecode/java_bytecode_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,19 @@
T read()
{
static_assert(
std::is_unsigned<T>::value, "T should be an unsigned integer");
std::is_unsigned<T>::value || std::is_signed<T>::value,
"T should be a signed or unsigned integer");

Check warning on line 136 in jbmc/src/java_bytecode/java_bytecode_parser.cpp

View check run for this annotation

Codecov / codecov/patch

jbmc/src/java_bytecode/java_bytecode_parser.cpp#L136

Added line #L136 was not covered by tests
const constexpr size_t bytes = sizeof(T);
u8 result = 0;
if(bytes == 1)
{
if(!*in)
{
log.error() << "unexpected end of bytecode file" << messaget::eom;
throw 0;

Check warning on line 143 in jbmc/src/java_bytecode/java_bytecode_parser.cpp

View check run for this annotation

Codecov / codecov/patch

jbmc/src/java_bytecode/java_bytecode_parser.cpp#L142-L143

Added lines #L142 - L143 were not covered by tests
}
return static_cast<T>(in->get());
}
T result = 0;
for(size_t i = 0; i < bytes; i++)
{
if(!*in)
Expand All @@ -145,7 +155,7 @@
result <<= 8u;
result |= static_cast<u1>(in->get());
}
return narrow_cast<T>(result);
return result;
}

void store_unknown_method_handle(size_t bootstrap_method_index);
Expand Down Expand Up @@ -700,7 +710,7 @@
std::string s;
s.resize(bytes);
for(auto &ch : s)
ch = read<u1>();
ch = read<std::string::value_type>();
it->s = s; // Add to string table
}
break;
Expand Down Expand Up @@ -942,15 +952,15 @@

case 'b': // a signed byte
{
const s1 c = read<u1>();
const s1 c = read<s1>();
instruction.args.push_back(from_integer(c, signedbv_typet(8)));
}
address+=1;
break;

case 'o': // two byte branch offset, signed
{
const s2 offset = read<u2>();
const s2 offset = read<s2>();
// By converting the signed offset into an absolute address (by adding
// the current address) the number represented becomes unsigned.
instruction.args.push_back(
Expand All @@ -961,7 +971,7 @@

case 'O': // four byte branch offset, signed
{
const s4 offset = read<u4>();
const s4 offset = read<s4>();

Check warning on line 974 in jbmc/src/java_bytecode/java_bytecode_parser.cpp

View check run for this annotation

Codecov / codecov/patch

jbmc/src/java_bytecode/java_bytecode_parser.cpp#L974

Added line #L974 was not covered by tests
// By converting the signed offset into an absolute address (by adding
// the current address) the number represented becomes unsigned.
instruction.args.push_back(
Expand Down Expand Up @@ -994,15 +1004,15 @@
{
const u2 v = read<u2>();
instruction.args.push_back(from_integer(v, unsignedbv_typet(16)));
const s2 c = read<u2>();
const s2 c = read<s2>();

Check warning on line 1007 in jbmc/src/java_bytecode/java_bytecode_parser.cpp

View check run for this annotation

Codecov / codecov/patch

jbmc/src/java_bytecode/java_bytecode_parser.cpp#L1007

Added line #L1007 was not covered by tests
instruction.args.push_back(from_integer(c, signedbv_typet(16)));
address+=4;
}
else // local variable index (one byte) plus one signed byte
{
const u1 v = read<u1>();
instruction.args.push_back(from_integer(v, unsignedbv_typet(8)));
const s1 c = read<u1>();
const s1 c = read<s1>();
instruction.args.push_back(from_integer(c, signedbv_typet(8)));
address+=2;
}
Expand Down Expand Up @@ -1032,7 +1042,7 @@
}

// now default value
const s4 default_value = read<u4>();
const s4 default_value = read<s4>();
// By converting the signed offset into an absolute address (by adding
// the current address) the number represented becomes unsigned.
instruction.args.push_back(
Expand All @@ -1045,8 +1055,8 @@

for(std::size_t i=0; i<npairs; i++)
{
const s4 match = read<u4>();
const s4 offset = read<u4>();
const s4 match = read<s4>();
const s4 offset = read<s4>();
instruction.args.push_back(
from_integer(match, signedbv_typet(32)));
// By converting the signed offset into an absolute address (by adding
Expand All @@ -1070,23 +1080,23 @@
}

// now default value
const s4 default_value = read<u4>();
const s4 default_value = read<s4>();
instruction.args.push_back(
from_integer(base_offset+default_value, signedbv_typet(32)));
address+=4;

// now low value
const s4 low_value = read<u4>();
const s4 low_value = read<s4>();
address+=4;

// now high value
const s4 high_value = read<u4>();
const s4 high_value = read<s4>();
address+=4;

// there are high-low+1 offsets, and they are signed
for(s4 i=low_value; i<=high_value; i++)
{
s4 offset = read<u4>();
s4 offset = read<s4>();
instruction.args.push_back(from_integer(i, signedbv_typet(32)));
// By converting the signed offset into an absolute address (by adding
// the current address) the number represented becomes unsigned.
Expand Down Expand Up @@ -1130,7 +1140,7 @@

case 's': // a signed short
{
const s2 s = read<u2>();
const s2 s = read<s2>();
instruction.args.push_back(from_integer(s, signedbv_typet(16)));
}
address+=2;
Expand Down
Loading