Skip to content

Commit

Permalink
Adding positive check
Browse files Browse the repository at this point in the history
  • Loading branch information
nagarev committed Dec 13, 2024
1 parent c7c2b2b commit 4efee38
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 9 additions & 1 deletion rskj-core/src/main/java/org/ethereum/vm/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ private void checkSizeArgument(long size) {
}
}

private int getPositiveInt(DataWord value) {
int intVal = value.intValue();
if (intVal < 0) {
throw Program.ExceptionHelper.notEnoughOpGas(program, op, Long.MAX_VALUE, program.getRemainingGas());
}
return intVal;
}

private long calcMemGas(long oldMemSize, long newMemSize, long copySize) {
long currentGasCost = 0;

Expand Down Expand Up @@ -1470,7 +1478,7 @@ protected void doMCOPY() {
hint = "dst: " + dst + " src: " + src + " length: " + length;
}

program.memoryCopy(dst.intValue(), src.intValue(), length.intValue());
program.memoryCopy(getPositiveInt(dst), getPositiveInt(src), getPositiveInt(length));
program.step();
}

Expand Down
13 changes: 11 additions & 2 deletions rskj-core/src/test/java/co/rsk/vm/opcode/MCopyInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void setup() {

@ParameterizedTest
@MethodSource("provideParametersForOOGCases")
void testMCopy_ShouldThrowOOGException(String[] initMemory, int dst, int src, long length) {
void testMCopy_ShouldThrowOOGException(String[] initMemory, long dst, long src, long length) {
// Given
byte[] code = compiler.compile("MCOPY");
VM vm = new VM(vmConfig, precompiledContracts);
Expand All @@ -77,9 +77,18 @@ void testMCopy_ShouldThrowOOGException(String[] initMemory, int dst, int src, lo

private static Stream<Arguments> provideParametersForOOGCases() {
return Stream.of(
// Special Border Cases
Arguments.of(new String[]{ "0000000000000000000000000000000000000000000000000000000000000000" }, 0, 0, -1),
Arguments.of(new String[]{}, 0, 0, -(2 * (Long.MAX_VALUE / 3))),
Arguments.of(new String[]{}, 0, 0, Integer.MAX_VALUE + 1L)
Arguments.of(new String[]{}, 0, 0, Integer.MAX_VALUE + 1L),
// Max Memory Limits
Arguments.of(new String[]{}, Program.MAX_MEMORY, 0, 1L),
Arguments.of(new String[]{}, 0, Program.MAX_MEMORY, 1L),
Arguments.of(new String[]{}, 0, 0, Program.MAX_MEMORY + 1),
// Negative Values
Arguments.of(new String[]{}, -1L, 0, 0),
Arguments.of(new String[]{}, 0, -1L, 0),
Arguments.of(new String[]{}, 0, 0, -1L)
);
}

Expand Down

0 comments on commit 4efee38

Please sign in to comment.