Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Improve and extend mappings check command. #334

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import cuchaz.enigma.Enigma;
import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.classprovider.ClasspathClassProvider;
import cuchaz.enigma.command.checks.CheckFailureException;
import cuchaz.enigma.command.checks.CheckInvalidMappings;
import cuchaz.enigma.command.checks.CheckNamedSyntheticEntry;
import cuchaz.enigma.command.checks.MappingCheck;
import cuchaz.enigma.command.checks.CheckPackageVisibility;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.representation.entry.ClassEntry;

import java.nio.file.Path;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.stream.Collectors;

public class CheckMappingsCommand extends Command {

Expand Down Expand Up @@ -50,29 +54,30 @@ public void run(String... args) throws Exception {
EntryTree<EntryMapping> mappings = format.read(fileMappings, ProgressListener.none(), saveParameters);
project.setMappings(mappings);

JarIndex idx = project.getJarIndex();

boolean error = false;

for (Set<ClassEntry> partition : idx.getPackageVisibilityIndex().getPartitions()) {
long packages = partition.stream()
.map(project.getMapper()::deobfuscate)
.map(ClassEntry::getPackageName)
.distinct()
.count();
if (packages > 1) {
error = true;
System.err.println("ERROR: Must be in one package:\n" + partition.stream()
.map(project.getMapper()::deobfuscate)
.map(ClassEntry::toString)
.sorted()
.collect(Collectors.joining("\n"))
);
}
Set<MappingCheck> checks = new HashSet<>();
checks.add(new CheckPackageVisibility());
checks.add(new CheckInvalidMappings());
checks.add(new CheckNamedSyntheticEntry());

LinkedList<CheckFailureException> errors = new LinkedList<>();
LinkedList<CheckFailureException> warnings = new LinkedList<>();

for (MappingCheck check : checks) {
check.findErrors(project, check.failOnError() ? errors : warnings);
}

if (error) {
throw new IllegalStateException("Errors in package visibility detected, see SysErr above");
System.out.printf("%d warnings:\n", warnings.size());
for (CheckFailureException warning : warnings) {
System.out.println(warning.getMessage());
}

System.out.printf("%d errors:\n", errors.size());
if (!errors.isEmpty()) {
for (CheckFailureException error : errors) {
System.err.println(error.getMessage());
}

throw new IllegalStateException("Mappings check failed, see above output");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cuchaz.enigma.command.checks;

public class CheckFailureException extends RuntimeException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally don't recommend overriding this. I see no point of this being an exception.

In fact, if the throwable/exception qualities are desirable, I suggest overriding fillInStackTrace to be no-op; otherwise the performance cost of creating exceptions are intensive, especially in loops

public CheckFailureException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cuchaz.enigma.command.checks;

import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.translation.representation.entry.Entry;

import java.util.Collection;

public class CheckInvalidMappings implements MappingCheck {
@Override
public void findErrors(EnigmaProject project, Collection<CheckFailureException> errors) {
Collection<Entry<?>> invalidEntries = project.dropMappings(ProgressListener.none());

for (Entry<?> invalidEntry : invalidEntries) {
errors.add(new CheckFailureException("Found invalid mapping entry: " + invalidEntry.toString()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cuchaz.enigma.command.checks;

import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.analysis.index.EntryIndex;
import cuchaz.enigma.translation.representation.entry.DefEntry;
import cuchaz.enigma.translation.representation.entry.Entry;

import java.util.Collection;

public class CheckNamedSyntheticEntry implements MappingCheck {
@Override
public void findErrors(EnigmaProject project, Collection<CheckFailureException> errors) {
EntryIndex entryIndex = project.getJarIndex().getEntryIndex();

check(entryIndex.getClasses(), project, errors);
check(entryIndex.getMethods(), project, errors);
check(entryIndex.getFields(), project, errors);
}

private void check(Collection<? extends Entry<?>> entries, EnigmaProject project, Collection<CheckFailureException> errors) {
for (Entry<?> entry : entries) {
DefEntry<?> defEntry = (DefEntry<?>) entry;
if (defEntry.getAccess().isSynthetic() && project.getMapper().hasDeobfMapping(entry)) {
errors.add(new CheckFailureException(String.format("Synthetic entry (%s) has a debof name (%s)", entry, project.getMapper().getDeobfMapping(entry).getTargetName())));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cuchaz.enigma.command.checks;

import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.translation.representation.entry.ClassEntry;

import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

public class CheckPackageVisibility implements MappingCheck {
@Override
public void findErrors(EnigmaProject project, Collection<CheckFailureException> errors) {
JarIndex idx = project.getJarIndex();

for (Set<ClassEntry> partition : idx.getPackageVisibilityIndex().getPartitions()) {
long packages = partition.stream()
.map(project.getMapper()::deobfuscate)
.map(ClassEntry::getPackageName)
.distinct()
.count();

if (packages > 1) {
errors.add(new CheckFailureException("ERROR: Must be in one package:\n" + partition.stream()
.map(project.getMapper()::deobfuscate)
.map(ClassEntry::toString)
.sorted()
.collect(Collectors.joining("\n"))
));
}
}
}

@Override
public boolean failOnError() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cuchaz.enigma.command.checks;

import cuchaz.enigma.EnigmaProject;

import java.util.Collection;

public interface MappingCheck {
void findErrors(EnigmaProject project, Collection<CheckFailureException> errors);

default boolean failOnError() {
return true;
}
}
4 changes: 3 additions & 1 deletion enigma/src/main/java/cuchaz/enigma/EnigmaProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@ public EntryRemapper getMapper() {
return mapper;
}

public void dropMappings(ProgressListener progress) {
public Collection<Entry<?>> dropMappings(ProgressListener progress) {
DeltaTrackingTree<EntryMapping> mappings = mapper.getObfToDeobf();

Collection<Entry<?>> dropped = dropMappings(mappings, progress);
for (Entry<?> entry : dropped) {
mappings.trackChange(entry);
}

return dropped;
}

private Collection<Entry<?>> dropMappings(EntryTree<EntryMapping> mappings, ProgressListener progress) {
Expand Down