Skip to content

Commit d5da212

Browse files
committed
Fix: convert assertions statements in Bytecode DSL processor with explicit checks
1 parent f56476e commit d5da212

File tree

7 files changed

+45
-19
lines changed

7 files changed

+45
-19
lines changed

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/generator/BytecodeRootNodeElement.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9113,11 +9113,12 @@ final class OperationVariables {
91139113
CodeVariableElement var;
91149114
if (fieldIndex < variables.size()) {
91159115
var = variables.get(fieldIndex);
9116-
} else {
9117-
assert fieldIndex == variables.size();
9116+
} else if (fieldIndex == variables.size()) {
91189117
String prefix = ElementUtils.firstLetterLowerCase(ElementUtils.getTypeSimpleId(fieldType));
91199118
var = new CodeVariableElement(Set.of(PRIVATE), fieldType, prefix + fieldIndex);
91209119
variables.add(var);
9120+
} else {
9121+
throw new AssertionError("fieldIndex %d exceeds variable size %d".formatted(fieldIndex, variables.size()));
91219122
}
91229123

91239124
this.fieldToVariable.put(field, var);

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/model/BytecodeDSLModel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,9 @@ public boolean hasYieldOperation() {
281281
public InstructionModel getInvalidateInstruction(int length) {
282282
if (invalidateInstructions == null) {
283283
return null;
284+
} else if (length % 2 != 0) {
285+
throw new AssertionError("instructions must be short-aligned");
284286
}
285-
assert length % 2 == 0;
286287
return invalidateInstructions[(length - OPCODE_WIDTH) / 2];
287288
}
288289

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/model/InstructionModel.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,15 @@ public InstructionEncoding getInstructionEncoding() {
639639
public String getInternalName() {
640640
String operationName = switch (kind) {
641641
case CUSTOM -> {
642-
assert name.startsWith("c.");
642+
if (!name.startsWith("c.")) {
643+
throw new AssertionError("Unexpected custom operation name: " + name);
644+
}
643645
yield name.substring(2) + "_";
644646
}
645647
case CUSTOM_SHORT_CIRCUIT -> {
646-
assert name.startsWith("sc.");
648+
if (!name.startsWith("sc.")) {
649+
throw new AssertionError("Unexpected short-circuit custom operation name: " + name);
650+
}
647651
yield name.substring(3) + "_";
648652
}
649653
default -> name;

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/model/OperationModel.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,18 @@ public OperationModel setOperationBeginArgumentVarArgs(boolean varArgs) {
274274
}
275275

276276
public OperationModel setOperationBeginArguments(OperationArgument... operationBeginArguments) {
277-
if (this.operationBeginArguments != null) {
278-
assert this.operationBeginArguments.length == operationBeginArguments.length;
277+
if (this.operationBeginArguments != EMPTY_ARGUMENTS && this.operationBeginArguments.length != operationBeginArguments.length) {
278+
throw new AssertionError("Number of begin arguments for %s should not change (was %d, attempted to set to %d).".formatted(name, this.operationBeginArguments.length,
279+
operationBeginArguments.length));
279280
}
280281
this.operationBeginArguments = operationBeginArguments;
281282
return this;
282283
}
283284

284285
public OperationModel setOperationEndArguments(OperationArgument... operationEndArguments) {
285-
if (this.operationEndArguments != null) {
286-
assert this.operationEndArguments.length == operationEndArguments.length;
286+
if (this.operationEndArguments != EMPTY_ARGUMENTS && this.operationEndArguments.length != operationEndArguments.length) {
287+
throw new AssertionError(
288+
"Number of end arguments for %s should not change (was %d, attempted to set to %d).".formatted(name, this.operationEndArguments.length, operationEndArguments.length));
287289
}
288290
this.operationEndArguments = operationEndArguments;
289291
return this;

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/parser/BytecodeDSLParser.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ protected BytecodeDSLModels parse(Element element, List<AnnotationMirror> mirror
132132
models = parseGenerateBytecodeTestVariants(typeElement, generateBytecodeTestVariantsMirror);
133133
} else {
134134
AnnotationMirror generateBytecodeMirror = ElementUtils.findAnnotationMirror(element.getAnnotationMirrors(), types.GenerateBytecode);
135-
assert generateBytecodeMirror != null;
135+
if (generateBytecodeMirror == null) {
136+
throw new AssertionError("No @%s mirror found.".formatted(getSimpleName(types.GenerateBytecode)));
137+
}
136138
topLevelAnnotationMirror = generateBytecodeMirror;
137139

138140
models = List.of(createBytecodeDSLModel(typeElement, generateBytecodeMirror, "Gen", types.BytecodeBuilder));
@@ -369,11 +371,15 @@ private void parseBytecodeDSLModel(TypeElement typeElement, BytecodeDSLModel mod
369371
// Prioritize a constructor that takes a FrameDescriptor.Builder.
370372
if (constructorsByFDType.containsKey(TruffleTypes.FrameDescriptor_Builder_Name)) {
371373
List<ExecutableElement> ctors = constructorsByFDType.get(TruffleTypes.FrameDescriptor_Builder_Name);
372-
assert ctors.size() == 1;
374+
if (ctors.size() != 1) {
375+
throw new AssertionError();
376+
}
373377
model.fdBuilderConstructor = ctors.get(0);
374378
} else {
375379
List<ExecutableElement> ctors = constructorsByFDType.get(TruffleTypes.FrameDescriptor_Name);
376-
assert ctors.size() == 1;
380+
if (ctors.size() != 1) {
381+
throw new AssertionError();
382+
}
377383
model.fdConstructor = ctors.get(0);
378384
}
379385

@@ -822,7 +828,9 @@ private void resolveBoxingElimination(BytecodeDSLModel model, List<QuickenDecisi
822828
List<ResolvedQuickenDecision> decisions = entry.getValue();
823829

824830
for (ResolvedQuickenDecision quickening : decisions) {
825-
assert !includedSpecializations.isEmpty();
831+
if (includedSpecializations.isEmpty()) {
832+
throw new AssertionError();
833+
}
826834
NodeData node = quickening.operation().instruction.nodeData;
827835

828836
String name;

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/parser/CustomOperationParser.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,9 @@ private void validateEpilogExceptionalSignature(CustomOperationModel customOpera
603603
}
604604

605605
private OperationArgument[] createOperationConstantArguments(List<ConstantOperandModel> operands, List<String> operandNames) {
606-
assert operands.size() == operandNames.size();
606+
if (operands.size() != operandNames.size()) {
607+
throw new AssertionError("Operands and operand names have different sizes (%d vs. %d)".formatted(operands.size(), operandNames.size()));
608+
}
607609
OperationArgument[] arguments = new OperationArgument[operandNames.size()];
608610
for (int i = 0; i < operandNames.size(); i++) {
609611
ConstantOperandModel constantOperand = operands.get(i);
@@ -673,7 +675,9 @@ private InstructionModel getOrCreateBooleanConverterInstruction(TypeElement type
673675
}
674676

675677
List<ExecutableElement> specializations = findSpecializations(typeElement);
676-
assert specializations.size() != 0;
678+
if (specializations.isEmpty()) {
679+
throw new AssertionError("Boolean converter should have at least one specialization if it parsed without error.");
680+
}
677681

678682
boolean returnsBoolean = true;
679683
for (ExecutableElement spec : specializations) {

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/parser/SpecializationSignatureParser.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,11 @@ private boolean checkConstantOperandParam(VariableElement constantOperandParam,
250250
* inspected individually.
251251
*/
252252
public static Signature createPolymorphicSignature(List<SpecializationSignature> signatures, List<ExecutableElement> specializations, MessageContainer customOperation) {
253-
assert !signatures.isEmpty();
254-
assert signatures.size() == specializations.size();
253+
if (signatures.isEmpty()) {
254+
throw new IllegalArgumentException("Cannot create a polymorphic signature for an empty list of signatures.");
255+
} else if (signatures.size() != specializations.size()) {
256+
throw new IllegalArgumentException("Number of signatures (%d) should match number of specializations (%d).".formatted(signatures.size(), specializations.size()));
257+
}
255258
Signature polymorphicSignature = signatures.get(0).signature();
256259
for (int i = 1; i < signatures.size(); i++) {
257260
polymorphicSignature = mergeSignatures(signatures.get(i).signature(), polymorphicSignature, specializations.get(i), customOperation);
@@ -276,8 +279,11 @@ private static Signature mergeSignatures(Signature a, Signature b, Element el, M
276279
}
277280
return null;
278281
}
279-
assert a.constantOperandsBeforeCount == b.constantOperandsBeforeCount;
280-
assert a.constantOperandsAfterCount == b.constantOperandsAfterCount;
282+
if (a.constantOperandsBeforeCount != b.constantOperandsBeforeCount) {
283+
throw new AssertionError("Constant operand before count differs between merged signatures (%d and %d).".formatted(a.constantOperandsBeforeCount, b.constantOperandsBeforeCount));
284+
} else if (a.constantOperandsAfterCount != b.constantOperandsAfterCount) {
285+
throw new AssertionError("Constant operand counts should match between merged signatures (%d and %d).".formatted(a.constantOperandsAfterCount, b.constantOperandsAfterCount));
286+
}
281287
if (a.dynamicOperandCount != b.dynamicOperandCount) {
282288
if (errorTarget != null) {
283289
errorTarget.addError(el, "Error calculating operation signature: all specializations must have the same number of operands.");

0 commit comments

Comments
 (0)