@Override public void visitStructDefinitionType(StructDefinitionType structDefinitionType) { if (structDefinitionType.hasStructNameType()) { visitChildFromParent(structDefinitionType.getStructNameType(), structDefinitionType); } for (String name : structDefinitionType.getFieldNames()) { visitChildFromParent(structDefinitionType.getFieldType(name), structDefinitionType); } }
@Override public boolean preconditionHolds() { return targetStruct.getNumFields() > 1 && targetStruct.hasField(fieldToRemove); } }
private StructDefinitionType makeStructDefinition(Optional<StructNameType> structNameType, Member_listContext memberListContext) { final Pair<List<String>, List<Type>> members = getMembers(memberListContext); return new StructDefinitionType(structNameType, members.a, members.b); }
@Override public void visitStructDefinitionType(StructDefinitionType structDefinitionType) { super.visitStructDefinitionType(structDefinitionType); if (structDefinitionType.hasStructNameType()) { assert !structDeclarationMap.containsKey(structDefinitionType.getStructNameType()); structDeclarationMap.put(structDefinitionType.getStructNameType(), structDefinitionType); } }
final int indexOfInlinedField = outerStruct.getFieldIndex(fieldToInline); outerStruct.removeField(fieldToInline); for (int i = 0; i < innerStruct.getNumFields(); i++) { final String oldFieldName = innerStruct.getFieldName(i); final String newFieldName = oldFieldName.startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX) ? fieldToInline + oldFieldName : oldFieldName; oldToNewFieldNames.put(oldFieldName, newFieldName); outerStruct.insertField(indexOfInlinedField + i, newFieldName, innerStruct.getFieldType(i));
private Expr makeInitializationExpr(StructDefinitionType structDefinitionType, Expr originalInitializer) { List<Expr> args = new ArrayList<>(); for (int i = 0; i < structDefinitionType.getNumFields(); i++) { if (structDefinitionType.getFieldName(i).startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX)) { final Type fieldType = structDefinitionType.getFieldType(i); args.add(fieldType instanceof StructNameType ? makeInitializationExpr(tu.getStructDefinition((StructNameType) fieldType), originalInitializer) : fieldType.getCanonicalConstant()); } else { args.add(originalInitializer); } } return new TypeConstructorExpr(structDefinitionType.getStructNameType().getName(), args); }
private void obfuscateTranslationUnit(TranslationUnit tu) { this.typer = new Typer(tu, tu.getShadingLanguageVersion()); visit(tu); for (VariableDeclInfo declInfo : varDeclMapping.keySet()) { assert varDeclMapping.containsKey(declInfo); declInfo.setName(varDeclMapping.get(declInfo)); } for (ParameterDecl parameterDecl : paramDeclMapping.keySet()) { assert paramDeclMapping.containsKey(parameterDecl); parameterDecl.setName(paramDeclMapping.get(parameterDecl)); } for (StructDefinitionType structDefinitionType : structDefinitionTypes) { if (structDefinitionType.hasStructNameType()) { final StructNameType structNameType = structDefinitionType.getStructNameType(); for (int i = 0; i < structDefinitionType.getNumFields(); i++) { assert structFieldRenaming.get(structNameType) .containsKey(structDefinitionType.getFieldName(i)); structDefinitionType.setFieldName(i, structFieldRenaming.get(structNameType) .get(structDefinitionType.getFieldName(i))); } } } new StandardVisitor() { @Override public void visitStructNameType(StructNameType structNameType) { structNameType.setName(namedStructRenaming.get(structNameType.getName())); } }.visit(tu); }
private Map<String, StructDefinitionType> getStructFields(StructDefinitionType struct) { return struct.getFieldNames().stream() .filter(item -> struct.getFieldType(item) instanceof StructNameType) .collect(Collectors.toMap( item -> item, item -> tu.getStructDefinition((StructNameType) struct.getFieldType(item))) ); }
@Override public void visitStructDefinitionType(StructDefinitionType structDefinitionType) { super.visitStructDefinitionType(structDefinitionType); // TODO: right now we do not obfuscate fields of anonymous structs. if (structDefinitionType.hasStructNameType()) { final String oldName = structDefinitionType.getStructNameType().getName(); final String newName = renameStruct(oldName); namedStructRenaming.put(oldName, newName); structDefinitionTypes.add(structDefinitionType); final Map<String, String> fieldRenaming = new HashMap<>(); for (String fieldName : structDefinitionType.getFieldNames()) { fieldRenaming.put(fieldName, renameStructField(fieldName)); } structFieldRenaming.put(structDefinitionType.getStructNameType(), fieldRenaming); } }
private void getOpportunitiesForStruct(StructNameType structType, VisitationDepth visitationDepth) { if (!(structType.getName().startsWith(Constants .STRUCTIFICATION_STRUCT_PREFIX))) { return; } final StructDefinitionType structDefinitionType = currentScope.lookupStructName(structType.getName()); for (String field : structDefinitionType.getFieldNames()) { if (!reachesOriginalVariable(structDefinitionType, field) && structDefinitionType.getNumFields() > 1) { final RemoveStructFieldReductionOpportunity op = new RemoveStructFieldReductionOpportunity( structDefinitionType, field, translationUnit, visitationDepth); if (op.preconditionHolds()) { opportunities.add(op); } } if (structDefinitionType.getFieldType(field).getWithoutQualifiers() instanceof StructNameType) { getOpportunitiesForStruct( (StructNameType) structDefinitionType.getFieldType(field).getWithoutQualifiers(), visitationDepth.deeper()); } } }
private List<String> getStructNames(List<Declaration> toBeAdded) { return toBeAdded .stream() .map(StructUtils::getStructDefinitions) .reduce(new ArrayList<>(), ListConcat::concatenate) .stream() .filter(StructDefinitionType::hasStructNameType) .map(item -> item.getStructNameType().getName()) .collect(Collectors.toList()); }
for (int i = 0; i < structDefinitionType.getNumFields(); i++) { .getArg(currentIndex))); if (structDefinitionType.getFieldName(i).startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX)) { Type fieldTypeWithoutQualifiers = structDefinitionType.getFieldType(i) .getWithoutQualifiers(); if (fieldTypeWithoutQualifiers instanceof StructNameType) { return Optional.of(new StructifiedVariableInfo(structDefinitionType.getFieldName(i), structDefinitionType.getFieldType(i), componentInitializer));
fieldTypes.add(newStructs.get(0).getStructNameType()); } else { result.add(new StructDefinitionType( new StructNameType(Constants.STRUCTIFICATION_STRUCT_PREFIX + idGenerator.freshId()), fieldNames, fieldTypes));
private Expr insertFieldIntoStruct(String enclosingStructName, StructDefinitionType enclosingStruct, IRandom generator) { Expr result = new VariableIdentifierExpr(enclosingStructName); StructDefinitionType currentStruct = enclosingStruct; while (true) { Map<String, StructDefinitionType> structFields = getStructFields(currentStruct); if (!structFields.keySet().isEmpty() && generator.nextBoolean()) { String fieldName = structFields.keySet().stream().collect(Collectors.toList()) .get(generator.nextInt(structFields.size())); result = new MemberLookupExpr(result, fieldName); currentStruct = structFields.get(fieldName); } else { // Choose random position at which to insert the field. currentStruct.insertField(generator.nextInt(currentStruct.getNumFields() + 1), declToTransform.getVariablesDeclaration().getDeclInfo(0).getName(), declToTransform.getVariablesDeclaration().getBaseType()); result = new MemberLookupExpr(result, declToTransform.getVariablesDeclaration() .getDeclInfo(0).getName()); return result; } } }
throw new RuntimeException("Could not find a struct named " + structName + " in scope."); assert sdt.getStructNameType().getName().equals(structName); return new TypeConstructorExpr(structName, sdt.getFieldTypes() .stream().map(item -> makeExpr(item, false, constContext, depth + 1)) .collect(Collectors.toList()));
public InlineStructifiedFieldReductionOpportunity(StructDefinitionType outerStruct, StructDefinitionType innerStruct, String fieldToInline, TranslationUnit tu, VisitationDepth depth) { super(depth); this.outerStruct = outerStruct; this.innerStruct = innerStruct; this.fieldToInline = fieldToInline; this.tu = tu; assert outerStruct != innerStruct; assert fieldToInline.startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX); assert outerStruct.getFieldType(fieldToInline).getWithoutQualifiers() instanceof StructNameType : "Can only inline a struct field of a struct"; }
private String unknownFieldMessage(String fieldName) { return "Field " + fieldName + " not found in struct" + (hasStructNameType() ? " " + structNameType : "") + "."; }
private boolean reachesOriginalVariable(StructDefinitionType structDefinitionType, String field) { if (!(field.startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX))) { return true; } if (!(structDefinitionType.getFieldType(field).getWithoutQualifiers() instanceof StructNameType)) { return false; } final StructNameType fieldType = (StructNameType) structDefinitionType.getFieldType(field).getWithoutQualifiers(); final StructDefinitionType nestedStruct = currentScope.lookupStructName(fieldType.getName()); return nestedStruct.getFieldNames().stream() .anyMatch(item -> reachesOriginalVariable(nestedStruct, item)); }
public void addStructDefinition(StructDefinitionType sdt) { assert sdt.hasStructNameType(); structMapping.put(sdt.getStructNameType().getName(), sdt); }
private Set<String> getStructNamesFromShader(TranslationUnit tu) { return tu.getStructDefinitions() .stream() .filter(StructDefinitionType::hasStructNameType) .map(item -> item.getStructNameType()) .filter(item -> item instanceof StructNameType) .map(item -> ((StructNameType) item).getName()) .collect(Collectors.toSet()); }