Skip to content

Commit

Permalink
feat: ✨ Decodificar opcodes invokeinterface e invokedynamic.
Browse files Browse the repository at this point in the history
  • Loading branch information
luigiminardim committed Jan 22, 2023
1 parent 17779f6 commit 4b66be8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
52 changes: 46 additions & 6 deletions src/Code.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ int Code__parse_lookupswitch_operands(
return pc - start_pc - 1;
}

int Code__parse_cpindex_byte_byte_operands(
Code *code, const char *mnemonic, u1 *bytes, u4 pc, int cpindex_num_bytes)
{
code->mnemonic = (char *)malloc(sizeof(char) * (strlen(mnemonic) + 1));
strcpy(code->mnemonic, mnemonic);
code->operand_type = OPERAND_TYPE_CPINDEX_BYTE_BYTE;
u2 cp_index = 0;
for (int i = 1; i <= cpindex_num_bytes; i++)
{
cp_index = cp_index << 8 | bytes[pc + i];
}
code->cpindex_byte_byte_operands.cpindex = cp_index;
code->cpindex_byte_byte_operands.byte1 = bytes[pc + cpindex_num_bytes + 1];
code->cpindex_byte_byte_operands.byte2 = bytes[pc + cpindex_num_bytes + 2];
return cpindex_num_bytes + 2;
}

Code *Code_Parse(u1 *bytes, u4 code_length)
{
Code *code = (Code *)malloc(sizeof(Code) * code_length);
Expand Down Expand Up @@ -708,12 +725,14 @@ Code *Code_Parse(u1 *bytes, u4 code_length)
case OPCODE_INVOKESTATIC:
op_size = Code__parse_cpindex_operands(&code[pc], "invokestatic", bytes, pc, 2);
break;
// case OPCODE_INVOKEINTERFACE:
// op_size = Code__parse_cpindex_operands(&code[pc], "invokeinterface", bytes, pc, 4);
// break;
// case OPCODE_INVOKEDYNAMIC:
// op_size = Code__parse_cpindex_operands(&code[pc], "invokedynamic", bytes, pc, 4);
// break;
case OPCODE_INVOKEINTERFACE:
op_size = Code__parse_cpindex_byte_byte_operands(
&code[pc], "invokeinterface", bytes, pc, 2);
break;
case OPCODE_INVOKEDYNAMIC:
op_size = Code__parse_cpindex_byte_byte_operands(
&code[pc], "invokedynamic", bytes, pc, 2);
break;
case OPCODE_NEW:
op_size = Code__parse_cpindex_operands(&code[pc], "new", bytes, pc, 2);
break;
Expand Down Expand Up @@ -913,6 +932,25 @@ char *Code__lookupswitch_operands_to_string(
return str;
}

char *Code__cpindex_byte_byte_operands_to_string(
Code code, ConstantPool constant_pool)
{
char *str = (char *)malloc(256 * sizeof(char));
char *cp_str = ConstantPool_get_utf8(
constant_pool, code.cpindex_byte_byte_operands.cpindex);
snprintf(
str, 256,
"%s #%hd %hhd %hhd // %s %hhd %hhd",
code.mnemonic, code.cpindex_byte_byte_operands.cpindex,
code.cpindex_byte_byte_operands.byte1,
code.cpindex_byte_byte_operands.byte2,
cp_str,
code.cpindex_byte_byte_operands.byte1,
code.cpindex_byte_byte_operands.byte2);
free(cp_str);
return str;
}

char *Code_entry_to_string(Code code_entry, ConstantPool constant_pool)
{
switch (code_entry.operand_type)
Expand All @@ -933,6 +971,8 @@ char *Code_entry_to_string(Code code_entry, ConstantPool constant_pool)
return Code__tableswitch_operands_to_string(code_entry, constant_pool);
case OPERAND_TYPE_LOOKUPSWITCH:
return Code__lookupswitch_operands_to_string(code_entry, constant_pool);
case OPERAND_TYPE_CPINDEX_BYTE_BYTE:
return Code__cpindex_byte_byte_operands_to_string(code_entry, constant_pool);
default:
char *final_str = (char *)malloc(2048 * sizeof(char));
snprintf(final_str, 2048, "0x%02X", code_entry.opcode);
Expand Down
13 changes: 11 additions & 2 deletions src/Code.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ enum Opcode
OPCODE_INVOKEVIRTUAL = 0xB6,
OPCODE_INVOKESPECIAL = 0xB7,
OPCODE_INVOKESTATIC = 0xB8,
// OPCODE_INVOKEINTERFACE = 0xB9,
// OPCODE_INVOKEDYNAMIC = 0xBA,
OPCODE_INVOKEINTERFACE = 0xB9,
OPCODE_INVOKEDYNAMIC = 0xBA,
OPCODE_NEW = 0xBB,
OPCODE_NEWARRAY = 0xBC,
OPCODE_ANEWARRAY = 0xBD,
Expand Down Expand Up @@ -226,6 +226,7 @@ enum OperandType
OPERAND_TYPE_CPINDEX_BYTE,
OPERAND_TYPE_TABLESWITCH,
OPERAND_TYPE_LOOKUPSWITCH,
OPERAND_TYPE_CPINDEX_BYTE_BYTE,
};

typedef struct
Expand Down Expand Up @@ -282,6 +283,13 @@ typedef struct
struct LookupswitchOperandsPairs *pairs;
} LookupswitchOperands;

typedef struct
{
u2 cpindex;
int8_t byte1;
int8_t byte2;
} CpindexByteByteOperands;

typedef struct
{
Opcode opcode;
Expand All @@ -297,6 +305,7 @@ typedef struct
IntOperands int_operands;
TableswitchOperands tableswitch_operands;
LookupswitchOperands lookupswitch_operands;
CpindexByteByteOperands cpindex_byte_byte_operands;
};
} Code;

Expand Down

0 comments on commit 4b66be8

Please sign in to comment.