GP-5367: Filter by max_primitives on homogeneous aggregate types in cspec

This commit is contained in:
ghintern 2025-07-14 17:21:02 +00:00
parent db1b6860c9
commit 3067d3e61f
7 changed files with 67 additions and 20 deletions

View file

@ -344,6 +344,7 @@
<define name="model_datatype_type">
<attribute name="name"/>
<optional><attribute name="maxprimitives"/></optional>
<optional><attribute name="minsize"/></optional>
<optional><attribute name="maxsize"/></optional>
<optional><attribute name="sizes"/></optional>

View file

@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -78,9 +78,9 @@ public interface DatatypeFilter {
if (nm.equals(SizeRestrictedFilter.NAME)) {
filter = new SizeRestrictedFilter();
}
else if (nm.equals(HomogeneousAggregate.NAME_FLOAT4)) {
filter = new HomogeneousAggregate(HomogeneousAggregate.NAME_FLOAT4,
PcodeDataTypeManager.TYPE_FLOAT, 4, 0, 0);
else if (nm.equals(HomogeneousAggregate.NAME_FLOAT)) {
filter = new HomogeneousAggregate(HomogeneousAggregate.NAME_FLOAT,
PcodeDataTypeManager.TYPE_FLOAT, HomogeneousAggregate.DEFAULT_MAX_PRIMITIVES, 0, 0);
}
else {
// If no other name matches, assume this is a decompiler metatype

View file

@ -19,10 +19,13 @@ import static ghidra.program.model.pcode.AttributeId.*;
import static ghidra.program.model.pcode.ElementId.*;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
import ghidra.program.model.data.DataType;
import ghidra.program.model.pcode.Encoder;
import ghidra.program.model.pcode.PcodeDataTypeManager;
import ghidra.util.xml.SpecXmlUtils;
import ghidra.xml.*;
/**
@ -31,8 +34,8 @@ import ghidra.xml.*;
*/
public class HomogeneousAggregate extends SizeRestrictedFilter {
public static final String NAME_FLOAT4 = "homogeneous-float-aggregate";
public static final int MAX_PRIMITIVES = 4; // Maximum number of primitives in aggregate data-type
public static final String NAME_FLOAT = "homogeneous-float-aggregate";
public static final int DEFAULT_MAX_PRIMITIVES = 4; // Maximum number of primitives in aggregate data-type
private String name;
private int metaType; // The expected meta-type
private int maxPrimitives; // Maximum number of primitives in the aggregate
@ -45,11 +48,11 @@ public class HomogeneousAggregate extends SizeRestrictedFilter {
public HomogeneousAggregate(String nm, int meta) {
name = nm;
metaType = meta;
maxPrimitives = 2;
maxPrimitives = DEFAULT_MAX_PRIMITIVES;
}
public HomogeneousAggregate(String nm, int meta, int maxPrim, int min, int max) {
super(min, max);
public HomogeneousAggregate(String nm, int meta, int maxPrim, int minSize, int maxSize) {
super(minSize, maxSize);
name = nm;
metaType = meta;
maxPrimitives = maxPrim;
@ -77,7 +80,7 @@ public class HomogeneousAggregate extends SizeRestrictedFilter {
if (meta != PcodeDataTypeManager.TYPE_ARRAY && meta != PcodeDataTypeManager.TYPE_STRUCT) {
return false;
}
PrimitiveExtractor primitives = new PrimitiveExtractor(dt, true, 0, MAX_PRIMITIVES);
PrimitiveExtractor primitives = new PrimitiveExtractor(dt, true, 0, maxPrimitives);
if (!primitives.isValid() || primitives.size() == 0 || primitives.containsUnknown() ||
!primitives.isAligned() || primitives.containsHoles()) {
return false;
@ -95,6 +98,12 @@ public class HomogeneousAggregate extends SizeRestrictedFilter {
return true;
}
@Override
protected void encodeAttributes(Encoder encoder) throws IOException {
super.encodeAttributes(encoder);
encoder.writeUnsignedInteger(ATTRIB_MAX_PRIMITIVES, maxPrimitives);
}
@Override
public void encode(Encoder encoder) throws IOException {
encoder.openElement(ELEM_DATATYPE);
@ -103,6 +112,22 @@ public class HomogeneousAggregate extends SizeRestrictedFilter {
encoder.closeElement(ELEM_DATATYPE);
}
@Override
protected void restoreAttributesXml(XmlElement el) throws XmlParseException {
super.restoreAttributesXml(el);
Iterator<Entry<String, String>> iter = el.getAttributes().entrySet().iterator();
while (iter.hasNext()) {
Entry<String, String> attrib = iter.next();
String nm = attrib.getKey();
if (nm.equals(ATTRIB_MAX_PRIMITIVES.name())) {
int xmlMaxPrim = SpecXmlUtils.decodeInt(attrib.getValue());
if (xmlMaxPrim > 0) {
maxPrimitives = xmlMaxPrim;
}
}
}
}
@Override
public void restoreXml(XmlPullParser parser) throws XmlParseException {
XmlElement elem = parser.start(ELEM_DATATYPE.name());

View file

@ -248,5 +248,7 @@ public record AttributeId(String name, int id) {
public static final AttributeId ATTRIB_SIZES = new AttributeId("sizes", 151);
public static final AttributeId ATTRIB_BACKFILL = new AttributeId("backfill", 152);
public static final AttributeId ATTRIB_UNKNOWN = new AttributeId("XMLunknown", 153);
public static final AttributeId ATTRIB_MAX_PRIMITIVES = new AttributeId("maxprimitives", 153);
public static final AttributeId ATTRIB_UNKNOWN = new AttributeId("XMLunknown", 154);
}