Skip to content

Commit

Permalink
Locals: more validation that local accesses are in scope
Browse files Browse the repository at this point in the history
  • Loading branch information
DSouzaM committed Oct 21, 2024
1 parent 88dca10 commit 3d873f3
Show file tree
Hide file tree
Showing 11 changed files with 606 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3926,7 +3926,7 @@ private void doLoadLocalMat(AbstractBytecodeNode $this, Frame stackFrame, Frame
int localRootIndex = BYTES.getShort(bc, bci + 4 /* imm root_index */);
BasicInterpreterBase localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
FRAMES.setObject(stackFrame, sp - 1, FRAMES.requireObject(frame, slot));
}
Expand All @@ -3937,7 +3937,7 @@ private void doStoreLocalMat(Frame stackFrame, Frame frame, byte[] bc, int bci,
int localRootIndex = BYTES.getShort(bc, bci + 4 /* imm root_index */);
BasicInterpreterBase localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
FRAMES.setObject(frame, slot, local);
FRAMES.clear(stackFrame, sp - 1);
Expand Down Expand Up @@ -6031,8 +6031,13 @@ public void emitLoadException() {
}

private void validateLocalScope(BytecodeLocal local) {
if (!((BytecodeLocalImpl) local).scope.valid) {
throw failArgument("Local variable scope of this local no longer valid.");
BytecodeLocalImpl localImpl = (BytecodeLocalImpl) local;
if (!localImpl.scope.valid) {
throw failArgument("Local variable scope of this local is no longer valid.");
}
RootData rootOperationData = getCurrentRootOperationData();
if (rootOperationData.index != localImpl.rootIndex) {
throw failArgument("Local variable must belong to the current root node. Consider using materialized local accesses to access locals from an outer root node.");
}
}

Expand Down Expand Up @@ -6065,6 +6070,13 @@ public void emitLoadLocal(BytecodeLocal local) {
afterChild(true, bci - 4);
}

private void validateMaterializedLocalScope(BytecodeLocal local) {
BytecodeLocalImpl localImpl = (BytecodeLocalImpl) local;
if (!localImpl.scope.valid) {
throw failArgument("Local variable scope of this local is no longer valid.");
}
}

/**
* Begins a built-in LoadLocalMaterialized operation.
* <p>
Expand Down Expand Up @@ -6092,7 +6104,7 @@ public void beginLoadLocalMaterialized(BytecodeLocal local) {
return;
}
validateRootOperationBegin();
validateLocalScope(local);
validateMaterializedLocalScope(local);
beforeChild();
BytecodeLocalImpl operationData = (BytecodeLocalImpl)local;
beginOperation(Operations.LOADLOCALMATERIALIZED, operationData);
Expand Down Expand Up @@ -6211,7 +6223,7 @@ public void beginStoreLocalMaterialized(BytecodeLocal local) {
return;
}
validateRootOperationBegin();
validateLocalScope(local);
validateMaterializedLocalScope(local);
beforeChild();
BytecodeLocalImpl operationData = (BytecodeLocalImpl)local;
beginOperation(Operations.STORELOCALMATERIALIZED, operationData);
Expand Down Expand Up @@ -8636,6 +8648,14 @@ private void validateRootOperationBegin() {
}
}

private RootData getCurrentRootOperationData() {
validateRootOperationBegin();
if (!(operationStack[rootOperationSp].data instanceof RootData rootOperationData)) {
throw assertionFailed("Data class RootData expected, but was " + operationStack[rootOperationSp].data);
}
return rootOperationData;
}

private void beforeChild() {
if (operationSp == 0) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3926,7 +3926,7 @@ private void doLoadLocalMat(AbstractBytecodeNode $this, Frame stackFrame, Frame
int localRootIndex = BYTES.getShort(bc, bci + 4 /* imm root_index */);
BasicInterpreterUnsafe localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
FRAMES.setObject(stackFrame, sp - 1, FRAMES.requireObject(frame, slot));
}
Expand All @@ -3937,7 +3937,7 @@ private void doStoreLocalMat(Frame stackFrame, Frame frame, byte[] bc, int bci,
int localRootIndex = BYTES.getShort(bc, bci + 4 /* imm root_index */);
BasicInterpreterUnsafe localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
FRAMES.setObject(frame, slot, local);
FRAMES.clear(stackFrame, sp - 1);
Expand Down Expand Up @@ -6031,8 +6031,13 @@ public void emitLoadException() {
}

private void validateLocalScope(BytecodeLocal local) {
if (!((BytecodeLocalImpl) local).scope.valid) {
throw failArgument("Local variable scope of this local no longer valid.");
BytecodeLocalImpl localImpl = (BytecodeLocalImpl) local;
if (!localImpl.scope.valid) {
throw failArgument("Local variable scope of this local is no longer valid.");
}
RootData rootOperationData = getCurrentRootOperationData();
if (rootOperationData.index != localImpl.rootIndex) {
throw failArgument("Local variable must belong to the current root node. Consider using materialized local accesses to access locals from an outer root node.");
}
}

Expand Down Expand Up @@ -6065,6 +6070,13 @@ public void emitLoadLocal(BytecodeLocal local) {
afterChild(true, bci - 4);
}

private void validateMaterializedLocalScope(BytecodeLocal local) {
BytecodeLocalImpl localImpl = (BytecodeLocalImpl) local;
if (!localImpl.scope.valid) {
throw failArgument("Local variable scope of this local is no longer valid.");
}
}

/**
* Begins a built-in LoadLocalMaterialized operation.
* <p>
Expand Down Expand Up @@ -6092,7 +6104,7 @@ public void beginLoadLocalMaterialized(BytecodeLocal local) {
return;
}
validateRootOperationBegin();
validateLocalScope(local);
validateMaterializedLocalScope(local);
beforeChild();
BytecodeLocalImpl operationData = (BytecodeLocalImpl)local;
beginOperation(Operations.LOADLOCALMATERIALIZED, operationData);
Expand Down Expand Up @@ -6211,7 +6223,7 @@ public void beginStoreLocalMaterialized(BytecodeLocal local) {
return;
}
validateRootOperationBegin();
validateLocalScope(local);
validateMaterializedLocalScope(local);
beforeChild();
BytecodeLocalImpl operationData = (BytecodeLocalImpl)local;
beginOperation(Operations.STORELOCALMATERIALIZED, operationData);
Expand Down Expand Up @@ -8636,6 +8648,14 @@ private void validateRootOperationBegin() {
}
}

private RootData getCurrentRootOperationData() {
validateRootOperationBegin();
if (!(operationStack[rootOperationSp].data instanceof RootData rootOperationData)) {
throw assertionFailed("Data class RootData expected, but was " + operationStack[rootOperationSp].data);
}
return rootOperationData;
}

private void beforeChild() {
if (operationSp == 0) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6474,7 +6474,7 @@ private void doLoadLocalMat(AbstractBytecodeNode $this, Frame stackFrame, Frame
int localIndex = BYTES.getShort(bc, bci + 6 /* imm local_index */);
BasicInterpreterWithBE localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
AbstractBytecodeNode bytecodeNode = localRoot.getBytecodeNodeImpl();
byte tag = bytecodeNode.getCachedLocalTagInternal(bytecodeNode.getLocalTags(), localIndex);
Expand Down Expand Up @@ -6520,7 +6520,7 @@ private void doLoadLocalMat(AbstractBytecodeNode $this, Frame stackFrame, Frame
int localIndex = BYTES.getShort(bc, bci + 6 /* imm local_index */);
BasicInterpreterWithBE localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
try {
FRAMES.setObject(stackFrame, sp - 1, FRAMES.expectBoolean(frame, slot));
Expand All @@ -6535,7 +6535,7 @@ private void doLoadLocalMat(AbstractBytecodeNode $this, Frame stackFrame, Frame
int localIndex = BYTES.getShort(bc, bci + 6 /* imm local_index */);
BasicInterpreterWithBE localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
try {
FRAMES.setBoolean(stackFrame, sp - 1, FRAMES.expectBoolean(frame, slot));
Expand All @@ -6550,7 +6550,7 @@ private void doLoadLocalMat(AbstractBytecodeNode $this, Frame stackFrame, Frame
int localIndex = BYTES.getShort(bc, bci + 6 /* imm local_index */);
BasicInterpreterWithBE localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
try {
FRAMES.setObject(stackFrame, sp - 1, FRAMES.expectLong(frame, slot));
Expand All @@ -6565,7 +6565,7 @@ private void doLoadLocalMat(AbstractBytecodeNode $this, Frame stackFrame, Frame
int localIndex = BYTES.getShort(bc, bci + 6 /* imm local_index */);
BasicInterpreterWithBE localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
try {
FRAMES.setLong(stackFrame, sp - 1, FRAMES.expectLong(frame, slot));
Expand All @@ -6580,7 +6580,7 @@ private void doLoadLocalMat(AbstractBytecodeNode $this, Frame stackFrame, Frame
int localIndex = BYTES.getShort(bc, bci + 6 /* imm local_index */);
BasicInterpreterWithBE localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
FRAMES.setObject(stackFrame, sp - 1, FRAMES.requireObject(frame, slot));
}
Expand All @@ -6593,7 +6593,7 @@ private void doStoreLocalMat(Frame stackFrame, Frame frame, byte[] bc, int bci,
int operandIndex = BYTES.getIntUnaligned(bc, bci + 8 /* imm child0 */);
BasicInterpreterWithBE localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
AbstractBytecodeNode bytecodeNode = localRoot.getBytecodeNodeImpl();
if (operandIndex != -1) {
Expand Down Expand Up @@ -6688,7 +6688,7 @@ private void doStoreLocalMat(Frame stackFrame, Frame frame, byte[] bc, int bci,
int localIndex = BYTES.getShort(bc, bci + 6 /* imm local_index */);
BasicInterpreterWithBE localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
AbstractBytecodeNode bytecodeNode = localRoot.getBytecodeNodeImpl();
byte tag = bytecodeNode.getCachedLocalTagInternal(bytecodeNode.getLocalTags(), localIndex);
Expand Down Expand Up @@ -6723,7 +6723,7 @@ private void doStoreLocalMat(Frame stackFrame, Frame frame, byte[] bc, int bci,
int localIndex = BYTES.getShort(bc, bci + 6 /* imm local_index */);
BasicInterpreterWithBE localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
AbstractBytecodeNode bytecodeNode = localRoot.getBytecodeNodeImpl();
byte tag = bytecodeNode.getCachedLocalTagInternal(bytecodeNode.getLocalTags(), localIndex);
Expand Down Expand Up @@ -6753,7 +6753,7 @@ private void doStoreLocalMat(Frame stackFrame, Frame frame, byte[] bc, int bci,
int localIndex = BYTES.getShort(bc, bci + 6 /* imm local_index */);
BasicInterpreterWithBE localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
AbstractBytecodeNode bytecodeNode = localRoot.getBytecodeNodeImpl();
byte tag = bytecodeNode.getCachedLocalTagInternal(bytecodeNode.getLocalTags(), localIndex);
Expand Down Expand Up @@ -6788,7 +6788,7 @@ private void doStoreLocalMat(Frame stackFrame, Frame frame, byte[] bc, int bci,
int localIndex = BYTES.getShort(bc, bci + 6 /* imm local_index */);
BasicInterpreterWithBE localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
AbstractBytecodeNode bytecodeNode = localRoot.getBytecodeNodeImpl();
byte tag = bytecodeNode.getCachedLocalTagInternal(bytecodeNode.getLocalTags(), localIndex);
Expand Down Expand Up @@ -6818,7 +6818,7 @@ private void doStoreLocalMat(Frame stackFrame, Frame frame, byte[] bc, int bci,
int localIndex = BYTES.getShort(bc, bci + 6 /* imm local_index */);
BasicInterpreterWithBE localRoot = this.getRoot().getBytecodeRootNodeImpl(localRootIndex);
if (localRoot.getFrameDescriptor() != frame.getFrameDescriptor()) {
throw CompilerDirectives.shouldNotReachHere("Materialized frame belongs to the wrong root node.");
throw new IllegalArgumentException("Materialized frame belongs to the wrong root node.");
}
FRAMES.setObject(frame, slot, local);
FRAMES.clear(stackFrame, sp - 1);
Expand Down Expand Up @@ -10061,8 +10061,13 @@ public void emitLoadException() {
}

private void validateLocalScope(BytecodeLocal local) {
if (!((BytecodeLocalImpl) local).scope.valid) {
throw failArgument("Local variable scope of this local no longer valid.");
BytecodeLocalImpl localImpl = (BytecodeLocalImpl) local;
if (!localImpl.scope.valid) {
throw failArgument("Local variable scope of this local is no longer valid.");
}
RootData rootOperationData = getCurrentRootOperationData();
if (rootOperationData.index != localImpl.rootIndex) {
throw failArgument("Local variable must belong to the current root node. Consider using materialized local accesses to access locals from an outer root node.");
}
}

Expand Down Expand Up @@ -10095,6 +10100,13 @@ public void emitLoadLocal(BytecodeLocal local) {
afterChild(true, bci - 6);
}

private void validateMaterializedLocalScope(BytecodeLocal local) {
BytecodeLocalImpl localImpl = (BytecodeLocalImpl) local;
if (!localImpl.scope.valid) {
throw failArgument("Local variable scope of this local is no longer valid.");
}
}

/**
* Begins a built-in LoadLocalMaterialized operation.
* <p>
Expand Down Expand Up @@ -10122,7 +10134,7 @@ public void beginLoadLocalMaterialized(BytecodeLocal local) {
return;
}
validateRootOperationBegin();
validateLocalScope(local);
validateMaterializedLocalScope(local);
beforeChild();
BytecodeLocalImpl operationData = (BytecodeLocalImpl)local;
beginOperation(Operations.LOADLOCALMATERIALIZED, operationData);
Expand Down Expand Up @@ -10241,7 +10253,7 @@ public void beginStoreLocalMaterialized(BytecodeLocal local) {
return;
}
validateRootOperationBegin();
validateLocalScope(local);
validateMaterializedLocalScope(local);
beforeChild();
StoreLocalData operationData = new StoreLocalData((BytecodeLocalImpl)local);
beginOperation(Operations.STORELOCALMATERIALIZED, operationData);
Expand Down Expand Up @@ -12684,6 +12696,14 @@ private void validateRootOperationBegin() {
}
}

private RootData getCurrentRootOperationData() {
validateRootOperationBegin();
if (!(operationStack[rootOperationSp].data instanceof RootData rootOperationData)) {
throw assertionFailed("Data class RootData expected, but was " + operationStack[rootOperationSp].data);
}
return rootOperationData;
}

private void beforeChild() {
if (operationSp == 0) {
return;
Expand Down
Loading

0 comments on commit 3d873f3

Please sign in to comment.