Skip to content

Commit

Permalink
feat(avm): Simulator enforces integral tag for DIV and field tag for …
Browse files Browse the repository at this point in the history
…FDIV (#9944)
  • Loading branch information
jeanmon authored and stevenplatt committed Nov 13, 2024
1 parent 081c1a1 commit 59389fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 0 additions & 1 deletion yarn-project/simulator/src/avm/opcodes/arithmetic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ describe('Arithmetic Instructions', () => {
});

describe.each([
[new Field(200n), new Field(99n), new Field(2n), TypeTag.FIELD],
[new Uint8(200n), new Uint8(99n), new Uint8(2n), TypeTag.UINT8],
[new Uint16(200n), new Uint16(99n), new Uint16(2n), TypeTag.UINT16],
[new Uint32(200n), new Uint32(99n), new Uint32(2n), TypeTag.UINT32],
Expand Down
23 changes: 21 additions & 2 deletions yarn-project/simulator/src/avm/opcodes/arithmetic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { AvmContext } from '../avm_context.js';
import { type Field, type MemoryValue } from '../avm_memory_types.js';
import {
type Field,
type MemoryValue,
TaggedMemory,
type TaggedMemoryInterface,
TypeTag,
} from '../avm_memory_types.js';
import { ArithmeticError } from '../errors.js';
import { Opcode } from '../serialization/instruction_serialization.js';
import { Addressing } from './addressing_mode.js';
Expand All @@ -13,7 +19,7 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst
const operands = [this.aOffset, this.bOffset, this.dstOffset];
const addressing = Addressing.fromWire(this.indirect, operands.length);
const [aOffset, bOffset, dstOffset] = addressing.resolve(operands, memory);
memory.checkTagsAreSame(aOffset, bOffset);
this.checkTags(memory, aOffset, bOffset);

const a = memory.get(aOffset);
const b = memory.get(bOffset);
Expand All @@ -25,6 +31,9 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst
}

protected abstract compute(a: MemoryValue, b: MemoryValue): MemoryValue;
protected checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) {
memory.checkTagsAreSame(aOffset, bOffset);
}
}

export class Add extends ThreeOperandArithmeticInstruction {
Expand Down Expand Up @@ -65,6 +74,11 @@ export class Div extends ThreeOperandArithmeticInstruction {

return a.div(b);
}

protected override checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) {
memory.checkTagsAreSame(aOffset, bOffset);
TaggedMemory.checkIsIntegralTag(memory.getTag(aOffset)); // Follows that bOffset tag is also of integral type
}
}

export class FieldDiv extends ThreeOperandArithmeticInstruction {
Expand All @@ -75,4 +89,9 @@ export class FieldDiv extends ThreeOperandArithmeticInstruction {
// return (a as Field).fdiv(b as Field);
return a.fdiv(b);
}

protected override checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) {
memory.checkTagsAreSame(aOffset, bOffset);
memory.checkTag(TypeTag.FIELD, aOffset); // Follows that bOffset has also tag of type Field
}
}

0 comments on commit 59389fb

Please sign in to comment.