Skip to content

Commit

Permalink
Improve error handling for floats in unions
Browse files Browse the repository at this point in the history
* We check for the correct setting (i.e., the memory model).
* We don't check for floats at the declaration of the union, but only at the write.
* Instead of crashing, we model it as an unsupported feature, i.e., we only report unknown if it can be reached.
  • Loading branch information
schuessf committed Nov 25, 2024
1 parent 9b3f7e0 commit ae94f3b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,6 @@ public Result visit(final IDispatcher main, final IASTCompositeTypeSpecifier nod
if (node.getKey() == IASTCompositeTypeSpecifier.k_struct) {
isStructOrUnion = StructOrUnion.STRUCT;
} else if (node.getKey() == IASTCompositeTypeSpecifier.k_union) {
// Currently the translation of unions that contain floats is unsound in the integer translation
// Therefore we throw an exception, s.t. we can continue in the bitvector translation, where this is
// correctly handled.
if (!mTranslationSettings.isBitvectorTranslation()
&& fTypes.stream().anyMatch(x -> x.getUnderlyingType().isFloatingType())) {
throw new UnsupportedSyntaxException(loc,
"floats in unions are not supported in the integer translation.");
}
isStructOrUnion = StructOrUnion.UNION;
} else {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2602,11 +2602,17 @@ private List<Statement> getWriteCallArray(final ILocation loc, final HeapLValue
private List<Statement> getWriteCallStruct(final ILocation loc, final HeapLValue hlv, final Expression value,
final CStructOrUnion valueType, final HeapWriteMode writeMode) {
final List<Statement> stmt = new ArrayList<>();
final boolean checkForFloats = CStructOrUnion.isUnion(valueType)
&& mSettings.getMemoryModelPreference() == MemoryModel.HoenickeLindenmann_Original;
for (final String fieldId : valueType.getFieldIds()) {
final Expression startAddress = hlv.getAddress();
final Expression newStartAddressBase = MemoryHandler.getPointerBaseAddress(startAddress, loc);
final Expression newStartAddressOffset = MemoryHandler.getPointerOffset(startAddress, loc);
final CType fieldType = valueType.getFieldType(fieldId);
if (checkForFloats && fieldType.getUnderlyingType().isFloatingType()) {
stmt.add(ExpressionTranslation.modelUnsupportedFeature(loc,
"write for union with floats in the HoenickeLindenmann_Original memory model"));
}
final StructAccessExpression sae = ExpressionFactory.constructStructAccessExpression(loc, value, fieldId);
final Offset fieldOffset = mTypeSizeAndOffsetComputer.constructOffsetForField(loc, valueType, fieldId);
if (fieldOffset.isBitfieldOffset()) {
Expand Down

0 comments on commit ae94f3b

Please sign in to comment.