Skip to content

Commit c24bced

Browse files
committed
C++: Add ReadSideEffectInstruction to IR
There was already a `WriteSideEffectInstruction` class that served as a superclass for all the specific write side effects. This new class serves the same purpose for read side effects.
1 parent 2aaf41a commit c24bced

File tree

5 files changed

+145
-90
lines changed

5 files changed

+145
-90
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,52 +1198,63 @@ class CallSideEffectInstruction extends SideEffectInstruction {
11981198
}
11991199

12001200
/**
1201-
* An instruction representing the side effect of a function call on any memory that might be read
1202-
* by that call.
1201+
* An instruction representing the side effect of a function call on any memory
1202+
* that might be read by that call. This instruction is emitted instead of
1203+
* `CallSideEffectInstruction` when it's certain that the call target cannot
1204+
* write to escaped memory.
12031205
*/
12041206
class CallReadSideEffectInstruction extends SideEffectInstruction {
12051207
CallReadSideEffectInstruction() { getOpcode() instanceof Opcode::CallReadSideEffect }
12061208
}
12071209

12081210
/**
1209-
* An instruction representing the read of an indirect parameter within a function call.
1211+
* An instruction representing a read side effect of a function call on a
1212+
* specific parameter.
12101213
*/
1211-
class IndirectReadSideEffectInstruction extends SideEffectInstruction {
1212-
IndirectReadSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectReadSideEffect }
1214+
class ReadSideEffectInstruction extends SideEffectInstruction, IndexedInstruction {
1215+
ReadSideEffectInstruction() { getOpcode() instanceof ReadSideEffectOpcode }
12131216

1214-
Instruction getArgumentDef() { result = getAnOperand().(AddressOperand).getDef() }
1217+
/** Gets the operand for the value that will be read from this instruction, if known. */
1218+
final SideEffectOperand getSideEffectOperand() { result = getAnOperand() }
1219+
1220+
/** Gets the value that will be read from this instruction, if known. */
1221+
final Instruction getSideEffect() { result = getSideEffectOperand().getDef() }
1222+
1223+
/** Gets the operand for the address from which this instruction may read. */
1224+
final AddressOperand getArgumentOperand() { result = getAnOperand() }
12151225

1216-
Instruction getSideEffect() { result = getAnOperand().(SideEffectOperand).getDef() }
1226+
/** Gets the address from which this instruction may read. */
1227+
final Instruction getArgumentDef() { result = getArgumentOperand().getDef() }
1228+
}
1229+
1230+
/**
1231+
* An instruction representing the read of an indirect parameter within a function call.
1232+
*/
1233+
class IndirectReadSideEffectInstruction extends ReadSideEffectInstruction {
1234+
IndirectReadSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectReadSideEffect }
12171235
}
12181236

12191237
/**
12201238
* An instruction representing the read of an indirect buffer parameter within a function call.
12211239
*/
1222-
class BufferReadSideEffectInstruction extends SideEffectInstruction {
1240+
class BufferReadSideEffectInstruction extends ReadSideEffectInstruction {
12231241
BufferReadSideEffectInstruction() { getOpcode() instanceof Opcode::BufferReadSideEffect }
1224-
1225-
Instruction getArgumentDef() { result = getAnOperand().(AddressOperand).getDef() }
1226-
1227-
Instruction getSideEffect() { result = getAnOperand().(SideEffectOperand).getDef() }
12281242
}
12291243

12301244
/**
12311245
* An instruction representing the read of an indirect buffer parameter within a function call.
12321246
*/
1233-
class SizedBufferReadSideEffectInstruction extends SideEffectInstruction {
1247+
class SizedBufferReadSideEffectInstruction extends ReadSideEffectInstruction {
12341248
SizedBufferReadSideEffectInstruction() {
12351249
getOpcode() instanceof Opcode::SizedBufferReadSideEffect
12361250
}
12371251

1238-
Instruction getArgumentDef() { result = getAnOperand().(AddressOperand).getDef() }
1239-
12401252
Instruction getSizeDef() { result = getAnOperand().(BufferSizeOperand).getDef() }
1241-
1242-
Instruction getSideEffect() { result = getAnOperand().(SideEffectOperand).getDef() }
12431253
}
12441254

12451255
/**
1246-
* An instruction representing a side effect of a function call.
1256+
* An instruction representing a write side effect of a function call on a
1257+
* specific parameter.
12471258
*/
12481259
class WriteSideEffectInstruction extends SideEffectInstruction, IndexedInstruction {
12491260
WriteSideEffectInstruction() { getOpcode() instanceof WriteSideEffectOpcode }

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,52 +1198,63 @@ class CallSideEffectInstruction extends SideEffectInstruction {
11981198
}
11991199

12001200
/**
1201-
* An instruction representing the side effect of a function call on any memory that might be read
1202-
* by that call.
1201+
* An instruction representing the side effect of a function call on any memory
1202+
* that might be read by that call. This instruction is emitted instead of
1203+
* `CallSideEffectInstruction` when it's certain that the call target cannot
1204+
* write to escaped memory.
12031205
*/
12041206
class CallReadSideEffectInstruction extends SideEffectInstruction {
12051207
CallReadSideEffectInstruction() { getOpcode() instanceof Opcode::CallReadSideEffect }
12061208
}
12071209

12081210
/**
1209-
* An instruction representing the read of an indirect parameter within a function call.
1211+
* An instruction representing a read side effect of a function call on a
1212+
* specific parameter.
12101213
*/
1211-
class IndirectReadSideEffectInstruction extends SideEffectInstruction {
1212-
IndirectReadSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectReadSideEffect }
1214+
class ReadSideEffectInstruction extends SideEffectInstruction, IndexedInstruction {
1215+
ReadSideEffectInstruction() { getOpcode() instanceof ReadSideEffectOpcode }
12131216

1214-
Instruction getArgumentDef() { result = getAnOperand().(AddressOperand).getDef() }
1217+
/** Gets the operand for the value that will be read from this instruction, if known. */
1218+
final SideEffectOperand getSideEffectOperand() { result = getAnOperand() }
1219+
1220+
/** Gets the value that will be read from this instruction, if known. */
1221+
final Instruction getSideEffect() { result = getSideEffectOperand().getDef() }
1222+
1223+
/** Gets the operand for the address from which this instruction may read. */
1224+
final AddressOperand getArgumentOperand() { result = getAnOperand() }
12151225

1216-
Instruction getSideEffect() { result = getAnOperand().(SideEffectOperand).getDef() }
1226+
/** Gets the address from which this instruction may read. */
1227+
final Instruction getArgumentDef() { result = getArgumentOperand().getDef() }
1228+
}
1229+
1230+
/**
1231+
* An instruction representing the read of an indirect parameter within a function call.
1232+
*/
1233+
class IndirectReadSideEffectInstruction extends ReadSideEffectInstruction {
1234+
IndirectReadSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectReadSideEffect }
12171235
}
12181236

12191237
/**
12201238
* An instruction representing the read of an indirect buffer parameter within a function call.
12211239
*/
1222-
class BufferReadSideEffectInstruction extends SideEffectInstruction {
1240+
class BufferReadSideEffectInstruction extends ReadSideEffectInstruction {
12231241
BufferReadSideEffectInstruction() { getOpcode() instanceof Opcode::BufferReadSideEffect }
1224-
1225-
Instruction getArgumentDef() { result = getAnOperand().(AddressOperand).getDef() }
1226-
1227-
Instruction getSideEffect() { result = getAnOperand().(SideEffectOperand).getDef() }
12281242
}
12291243

12301244
/**
12311245
* An instruction representing the read of an indirect buffer parameter within a function call.
12321246
*/
1233-
class SizedBufferReadSideEffectInstruction extends SideEffectInstruction {
1247+
class SizedBufferReadSideEffectInstruction extends ReadSideEffectInstruction {
12341248
SizedBufferReadSideEffectInstruction() {
12351249
getOpcode() instanceof Opcode::SizedBufferReadSideEffect
12361250
}
12371251

1238-
Instruction getArgumentDef() { result = getAnOperand().(AddressOperand).getDef() }
1239-
12401252
Instruction getSizeDef() { result = getAnOperand().(BufferSizeOperand).getDef() }
1241-
1242-
Instruction getSideEffect() { result = getAnOperand().(SideEffectOperand).getDef() }
12431253
}
12441254

12451255
/**
1246-
* An instruction representing a side effect of a function call.
1256+
* An instruction representing a write side effect of a function call on a
1257+
* specific parameter.
12471258
*/
12481259
class WriteSideEffectInstruction extends SideEffectInstruction, IndexedInstruction {
12491260
WriteSideEffectInstruction() { getOpcode() instanceof WriteSideEffectOpcode }

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,52 +1198,63 @@ class CallSideEffectInstruction extends SideEffectInstruction {
11981198
}
11991199

12001200
/**
1201-
* An instruction representing the side effect of a function call on any memory that might be read
1202-
* by that call.
1201+
* An instruction representing the side effect of a function call on any memory
1202+
* that might be read by that call. This instruction is emitted instead of
1203+
* `CallSideEffectInstruction` when it's certain that the call target cannot
1204+
* write to escaped memory.
12031205
*/
12041206
class CallReadSideEffectInstruction extends SideEffectInstruction {
12051207
CallReadSideEffectInstruction() { getOpcode() instanceof Opcode::CallReadSideEffect }
12061208
}
12071209

12081210
/**
1209-
* An instruction representing the read of an indirect parameter within a function call.
1211+
* An instruction representing a read side effect of a function call on a
1212+
* specific parameter.
12101213
*/
1211-
class IndirectReadSideEffectInstruction extends SideEffectInstruction {
1212-
IndirectReadSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectReadSideEffect }
1214+
class ReadSideEffectInstruction extends SideEffectInstruction, IndexedInstruction {
1215+
ReadSideEffectInstruction() { getOpcode() instanceof ReadSideEffectOpcode }
12131216

1214-
Instruction getArgumentDef() { result = getAnOperand().(AddressOperand).getDef() }
1217+
/** Gets the operand for the value that will be read from this instruction, if known. */
1218+
final SideEffectOperand getSideEffectOperand() { result = getAnOperand() }
1219+
1220+
/** Gets the value that will be read from this instruction, if known. */
1221+
final Instruction getSideEffect() { result = getSideEffectOperand().getDef() }
1222+
1223+
/** Gets the operand for the address from which this instruction may read. */
1224+
final AddressOperand getArgumentOperand() { result = getAnOperand() }
12151225

1216-
Instruction getSideEffect() { result = getAnOperand().(SideEffectOperand).getDef() }
1226+
/** Gets the address from which this instruction may read. */
1227+
final Instruction getArgumentDef() { result = getArgumentOperand().getDef() }
1228+
}
1229+
1230+
/**
1231+
* An instruction representing the read of an indirect parameter within a function call.
1232+
*/
1233+
class IndirectReadSideEffectInstruction extends ReadSideEffectInstruction {
1234+
IndirectReadSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectReadSideEffect }
12171235
}
12181236

12191237
/**
12201238
* An instruction representing the read of an indirect buffer parameter within a function call.
12211239
*/
1222-
class BufferReadSideEffectInstruction extends SideEffectInstruction {
1240+
class BufferReadSideEffectInstruction extends ReadSideEffectInstruction {
12231241
BufferReadSideEffectInstruction() { getOpcode() instanceof Opcode::BufferReadSideEffect }
1224-
1225-
Instruction getArgumentDef() { result = getAnOperand().(AddressOperand).getDef() }
1226-
1227-
Instruction getSideEffect() { result = getAnOperand().(SideEffectOperand).getDef() }
12281242
}
12291243

12301244
/**
12311245
* An instruction representing the read of an indirect buffer parameter within a function call.
12321246
*/
1233-
class SizedBufferReadSideEffectInstruction extends SideEffectInstruction {
1247+
class SizedBufferReadSideEffectInstruction extends ReadSideEffectInstruction {
12341248
SizedBufferReadSideEffectInstruction() {
12351249
getOpcode() instanceof Opcode::SizedBufferReadSideEffect
12361250
}
12371251

1238-
Instruction getArgumentDef() { result = getAnOperand().(AddressOperand).getDef() }
1239-
12401252
Instruction getSizeDef() { result = getAnOperand().(BufferSizeOperand).getDef() }
1241-
1242-
Instruction getSideEffect() { result = getAnOperand().(SideEffectOperand).getDef() }
12431253
}
12441254

12451255
/**
1246-
* An instruction representing a side effect of a function call.
1256+
* An instruction representing a write side effect of a function call on a
1257+
* specific parameter.
12471258
*/
12481259
class WriteSideEffectInstruction extends SideEffectInstruction, IndexedInstruction {
12491260
WriteSideEffectInstruction() { getOpcode() instanceof WriteSideEffectOpcode }

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/Instruction.qll

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,52 +1198,63 @@ class CallSideEffectInstruction extends SideEffectInstruction {
11981198
}
11991199

12001200
/**
1201-
* An instruction representing the side effect of a function call on any memory that might be read
1202-
* by that call.
1201+
* An instruction representing the side effect of a function call on any memory
1202+
* that might be read by that call. This instruction is emitted instead of
1203+
* `CallSideEffectInstruction` when it's certain that the call target cannot
1204+
* write to escaped memory.
12031205
*/
12041206
class CallReadSideEffectInstruction extends SideEffectInstruction {
12051207
CallReadSideEffectInstruction() { getOpcode() instanceof Opcode::CallReadSideEffect }
12061208
}
12071209

12081210
/**
1209-
* An instruction representing the read of an indirect parameter within a function call.
1211+
* An instruction representing a read side effect of a function call on a
1212+
* specific parameter.
12101213
*/
1211-
class IndirectReadSideEffectInstruction extends SideEffectInstruction {
1212-
IndirectReadSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectReadSideEffect }
1214+
class ReadSideEffectInstruction extends SideEffectInstruction, IndexedInstruction {
1215+
ReadSideEffectInstruction() { getOpcode() instanceof ReadSideEffectOpcode }
12131216

1214-
Instruction getArgumentDef() { result = getAnOperand().(AddressOperand).getDef() }
1217+
/** Gets the operand for the value that will be read from this instruction, if known. */
1218+
final SideEffectOperand getSideEffectOperand() { result = getAnOperand() }
1219+
1220+
/** Gets the value that will be read from this instruction, if known. */
1221+
final Instruction getSideEffect() { result = getSideEffectOperand().getDef() }
1222+
1223+
/** Gets the operand for the address from which this instruction may read. */
1224+
final AddressOperand getArgumentOperand() { result = getAnOperand() }
12151225

1216-
Instruction getSideEffect() { result = getAnOperand().(SideEffectOperand).getDef() }
1226+
/** Gets the address from which this instruction may read. */
1227+
final Instruction getArgumentDef() { result = getArgumentOperand().getDef() }
1228+
}
1229+
1230+
/**
1231+
* An instruction representing the read of an indirect parameter within a function call.
1232+
*/
1233+
class IndirectReadSideEffectInstruction extends ReadSideEffectInstruction {
1234+
IndirectReadSideEffectInstruction() { getOpcode() instanceof Opcode::IndirectReadSideEffect }
12171235
}
12181236

12191237
/**
12201238
* An instruction representing the read of an indirect buffer parameter within a function call.
12211239
*/
1222-
class BufferReadSideEffectInstruction extends SideEffectInstruction {
1240+
class BufferReadSideEffectInstruction extends ReadSideEffectInstruction {
12231241
BufferReadSideEffectInstruction() { getOpcode() instanceof Opcode::BufferReadSideEffect }
1224-
1225-
Instruction getArgumentDef() { result = getAnOperand().(AddressOperand).getDef() }
1226-
1227-
Instruction getSideEffect() { result = getAnOperand().(SideEffectOperand).getDef() }
12281242
}
12291243

12301244
/**
12311245
* An instruction representing the read of an indirect buffer parameter within a function call.
12321246
*/
1233-
class SizedBufferReadSideEffectInstruction extends SideEffectInstruction {
1247+
class SizedBufferReadSideEffectInstruction extends ReadSideEffectInstruction {
12341248
SizedBufferReadSideEffectInstruction() {
12351249
getOpcode() instanceof Opcode::SizedBufferReadSideEffect
12361250
}
12371251

1238-
Instruction getArgumentDef() { result = getAnOperand().(AddressOperand).getDef() }
1239-
12401252
Instruction getSizeDef() { result = getAnOperand().(BufferSizeOperand).getDef() }
1241-
1242-
Instruction getSideEffect() { result = getAnOperand().(SideEffectOperand).getDef() }
12431253
}
12441254

12451255
/**
1246-
* An instruction representing a side effect of a function call.
1256+
* An instruction representing a write side effect of a function call on a
1257+
* specific parameter.
12471258
*/
12481259
class WriteSideEffectInstruction extends SideEffectInstruction, IndexedInstruction {
12491260
WriteSideEffectInstruction() { getOpcode() instanceof WriteSideEffectOpcode }

0 commit comments

Comments
 (0)