Commit a328bf64 authored by pcregut's avatar pcregut

Check that indices are correct before writing them out.

parent b34efe51
...@@ -151,7 +151,24 @@ public abstract class Instruction { ...@@ -151,7 +151,24 @@ public abstract class Instruction {
} }
} }
/** /**
* Tests if the given number can be held in 8 bits, throw an IllegalArgumentException otherwise.
*/
public static void test8BitsLimit(int number) {
if ((number & 0xffffff00) != 0) {
throw new IllegalArgumentException("The number " + number +" can't be held in 8 bits !");
}
}
/**
* Tests if the given number can be held in 16 bits, throw an IllegalArgumentException otherwise.
*/
public static void test16BitsLimit(int number) {
if ((number & 0xffff0000) != 0) {
throw new IllegalArgumentException("The number " + number +" can't be held in 16 bits !");
}
}
/**
* Tests if the given numbers can be held in 4 bits, throw an IllegalArgumentException otherwise. * Tests if the given numbers can be held in 4 bits, throw an IllegalArgumentException otherwise.
*/ */
public static void test4BitsLimit(int[] numbers) { public static void test4BitsLimit(int[] numbers) {
...@@ -190,4 +207,5 @@ public abstract class Instruction { ...@@ -190,4 +207,5 @@ public abstract class Instruction {
public static byte getInstructionSizeInByte(int opcode) { public static byte getInstructionSizeInByte(int opcode) {
return instructionSizeInBytes[opcode]; return instructionSizeInBytes[opcode];
} }
} }
...@@ -98,6 +98,7 @@ implements IOneRegisterInstruction, ILiteralInstruction { ...@@ -98,6 +98,7 @@ implements IOneRegisterInstruction, ILiteralInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test4BitsLimit(registerA);
int firstShort = ((literalB & 0xf) << 12) + ((registerA & 0xf) << 8) + opcodeByte; int firstShort = ((literalB & 0xf) << 12) + ((registerA & 0xf) << 8) + opcodeByte;
out.putShort(firstShort); out.putShort(firstShort);
} }
......
...@@ -105,6 +105,7 @@ public class InstructionFormat11X extends Instruction implements IOneRegisterIns ...@@ -105,6 +105,7 @@ public class InstructionFormat11X extends Instruction implements IOneRegisterIns
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test8BitsLimit(registerA);
out.putShort(((registerA & 0xff) << 8) + opcodeByte); out.putShort(((registerA & 0xff) << 8) + opcodeByte);
} }
......
...@@ -139,6 +139,7 @@ public class InstructionFormat12X extends Instruction implements ITwoRegistersIn ...@@ -139,6 +139,7 @@ public class InstructionFormat12X extends Instruction implements ITwoRegistersIn
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test4BitsLimit(registerA | registerB);
int firstShort = ((registerB & 0xf) << 12) + ((registerA & 0xf) << 8) + opcodeByte; int firstShort = ((registerB & 0xf) << 12) + ((registerA & 0xf) << 8) + opcodeByte;
out.putShort(firstShort); out.putShort(firstShort);
} }
......
...@@ -162,6 +162,7 @@ implements IOneRegisterInstruction, IIndexInstruction { ...@@ -162,6 +162,7 @@ implements IOneRegisterInstruction, IIndexInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test8BitsLimit(registerA);
// The format is AA|op BBBB. // The format is AA|op BBBB.
out.putShort(((registerA & 0xff) << 8) + opcodeByte); out.putShort(((registerA & 0xff) << 8) + opcodeByte);
// The index may be a Type, or a String index. // The index may be a Type, or a String index.
......
...@@ -138,6 +138,7 @@ implements IOneRegisterInstruction, ILiteralInstruction { ...@@ -138,6 +138,7 @@ implements IOneRegisterInstruction, ILiteralInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test8BitsLimit(registerA);
out.putShort(((registerA & 0xff) << 8) + opcodeByte); out.putShort(((registerA & 0xff) << 8) + opcodeByte);
out.putShort(literalB & 0xffff); out.putShort(literalB & 0xffff);
} }
......
...@@ -133,6 +133,7 @@ implements IOneRegisterInstruction, ILiteralInstruction { ...@@ -133,6 +133,7 @@ implements IOneRegisterInstruction, ILiteralInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test8BitsLimit(registerA);
out.putShort(((registerA & 0xff) << 8) + opcodeByte); out.putShort(((registerA & 0xff) << 8) + opcodeByte);
out.putShort(literalB & 0xffff); out.putShort(literalB & 0xffff);
} }
......
...@@ -162,6 +162,7 @@ implements IOneRegisterInstruction, IOffsetInstruction { ...@@ -162,6 +162,7 @@ implements IOneRegisterInstruction, IOffsetInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test8BitsLimit(registerA);
// The format is AA|op BBBB. // The format is AA|op BBBB.
int firstShort = ((registerA & 0xff) << 8) + opcodeByte; int firstShort = ((registerA & 0xff) << 8) + opcodeByte;
int secondShort = (label.getOffset() - instructionOffset) / 2; // / 2 because offsets are word-based. int secondShort = (label.getOffset() - instructionOffset) / 2; // / 2 because offsets are word-based.
......
...@@ -158,6 +158,7 @@ implements ITwoRegistersInstruction, ILiteralInstruction { ...@@ -158,6 +158,7 @@ implements ITwoRegistersInstruction, ILiteralInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test8BitsLimit(registerA | registerB);
out.putShort(((registerA & 0xff) << 8) + opcodeByte); out.putShort(((registerA & 0xff) << 8) + opcodeByte);
out.putShort(((literalC & 0xff) << 8) + (registerB & 0xff)); out.putShort(((literalC & 0xff) << 8) + (registerB & 0xff));
} }
......
...@@ -189,6 +189,7 @@ implements ITwoRegistersInstruction, IIndexInstruction { ...@@ -189,6 +189,7 @@ implements ITwoRegistersInstruction, IIndexInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
// The format is B|A|op CCCC. // The format is B|A|op CCCC.
test8BitsLimit(registerA | registerB);
out.putShort((((registerA & 0xf) << 8) + ((registerB & 0xf) << 12)) + opcodeByte); out.putShort((((registerA & 0xf) << 8) + ((registerB & 0xf) << 12)) + opcodeByte);
// The index may be a Type, or a Field index. // The index may be a Type, or a Field index.
int index; int index;
......
...@@ -147,6 +147,7 @@ implements ITwoRegistersInstruction, ILiteralInstruction { ...@@ -147,6 +147,7 @@ implements ITwoRegistersInstruction, ILiteralInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test4BitsLimit(registerA | registerB);
out.putShort((((registerA & 0xf) << 8) + ((registerB & 0xf) << 12)) + opcodeByte); out.putShort((((registerA & 0xf) << 8) + ((registerB & 0xf) << 12)) + opcodeByte);
out.putShort(literalC & 0xffff); out.putShort(literalC & 0xffff);
} }
......
...@@ -183,6 +183,8 @@ implements ITwoRegistersInstruction, IOffsetInstruction { ...@@ -183,6 +183,8 @@ implements ITwoRegistersInstruction, IOffsetInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test4BitsLimit(registerA | registerB);
// The format is B|A|op CCCC. // The format is B|A|op CCCC.
int firstShort = ((registerB & 0xf) << 12) + ((registerA & 0xf) << 8) + opcodeByte; int firstShort = ((registerB & 0xf) << 12) + ((registerA & 0xf) << 8) + opcodeByte;
int secondShort = (label.getOffset() - instructionOffset) / 2; // / 2 because offsets are word-based. int secondShort = (label.getOffset() - instructionOffset) / 2; // / 2 because offsets are word-based.
......
...@@ -134,6 +134,8 @@ implements ITwoRegistersInstruction { ...@@ -134,6 +134,8 @@ implements ITwoRegistersInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test8BitsLimit(registerA);
test16BitsLimit(registerB);
int firstShort = ((registerA & 0xff) << 8) + opcodeByte; int firstShort = ((registerA & 0xff) << 8) + opcodeByte;
out.putShort(firstShort); out.putShort(firstShort);
out.putShort(registerB & 0xffff); out.putShort(registerB & 0xffff);
......
...@@ -197,6 +197,7 @@ implements IThreeRegistersInstruction { ...@@ -197,6 +197,7 @@ implements IThreeRegistersInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test8BitsLimit(registerA | registerB | registerC);
out.putShort(((registerA & 0xff) << 8) + opcodeByte); out.putShort(((registerA & 0xff) << 8) + opcodeByte);
out.putShort(((registerC & 0xff) << 8) + (registerB & 0xff)); out.putShort(((registerC & 0xff) << 8) + (registerB & 0xff));
} }
......
...@@ -132,6 +132,7 @@ implements IOneRegisterInstruction, IIndexInstruction { ...@@ -132,6 +132,7 @@ implements IOneRegisterInstruction, IIndexInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test8BitsLimit(registerA);
// The format is AA|op BBBBlo BBBBhi. // The format is AA|op BBBBlo BBBBhi.
out.putShort(((registerA & 0xff) << 8) + opcodeByte); out.putShort(((registerA & 0xff) << 8) + opcodeByte);
int index = constantPool.getStringIndex(string); int index = constantPool.getStringIndex(string);
......
...@@ -134,6 +134,7 @@ implements IOneRegisterInstruction, ILiteralInstruction { ...@@ -134,6 +134,7 @@ implements IOneRegisterInstruction, ILiteralInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test8BitsLimit(registerA);
out.putShort(((registerA & 0xff) << 8) + opcodeByte); out.putShort(((registerA & 0xff) << 8) + opcodeByte);
out.putShort(literalB & 0xffff); out.putShort(literalB & 0xffff);
out.putShort((literalB >> 16) & 0xffff); out.putShort((literalB >> 16) & 0xffff);
......
...@@ -168,6 +168,7 @@ implements IOneRegisterInstruction, IOffsetInstruction { ...@@ -168,6 +168,7 @@ implements IOneRegisterInstruction, IOffsetInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
// The format is AA|op BBBBlo BBBBhi. // The format is AA|op BBBBlo BBBBhi.
test8BitsLimit(registerA);
out.putShort(((registerA & 0xff) << 8) + opcodeByte); out.putShort(((registerA & 0xff) << 8) + opcodeByte);
int branchOffset = (label.getOffset() - instructionOffset) / 2; // / 2 because offsets are word-based. int branchOffset = (label.getOffset() - instructionOffset) / 2; // / 2 because offsets are word-based.
out.putShort(branchOffset & 0xffff); out.putShort(branchOffset & 0xffff);
......
...@@ -136,6 +136,7 @@ implements ITwoRegistersInstruction { ...@@ -136,6 +136,7 @@ implements ITwoRegistersInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test16BitsLimit(registerA | registerB);
out.putShort(opcodeByte); out.putShort(opcodeByte);
out.putShort(registerA & 0xffff); out.putShort(registerA & 0xffff);
out.putShort(registerB & 0xffff); out.putShort(registerB & 0xffff);
......
...@@ -200,7 +200,9 @@ implements IRegisterArrayInstruction, IIndexInstruction { ...@@ -200,7 +200,9 @@ implements IRegisterArrayInstruction, IIndexInstruction {
try { throw new Exception("Abnormal arguments number : " + nbRegisters); } try { throw new Exception("Abnormal arguments number : " + nbRegisters); }
catch (Exception e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }
} }
int mask=0;
for(int i=0; i < nbRegisters; i++) mask |= registers[i];
test4BitsLimit(mask);
out.putShort(firstShort); out.putShort(firstShort);
if (opcodeByte == 0x24) { if (opcodeByte == 0x24) {
out.putShort(constantPool.getTypeIndex(type)); out.putShort(constantPool.getTypeIndex(type));
......
...@@ -141,6 +141,7 @@ implements IOneRegisterInstruction, ILongLiteralInstruction { ...@@ -141,6 +141,7 @@ implements IOneRegisterInstruction, ILongLiteralInstruction {
@Override @Override
public void write(ByteVector out, ConstantPool constantPool) { public void write(ByteVector out, ConstantPool constantPool) {
test8BitsLimit(registerA);
out.putShort(((registerA & 0xff) << 8) + opcodeByte); out.putShort(((registerA & 0xff) << 8) + opcodeByte);
out.putByte((byte)literalB); out.putByte((byte)literalB);
out.putByte((byte)(literalB >> 8)); out.putByte((byte)(literalB >> 8));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment