-
Notifications
You must be signed in to change notification settings - Fork 47
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
Allow ILaunchPluginService to filter handling classes based on constant pool #122
Draft
embeddedt
wants to merge
2
commits into
McModLauncher:main
Choose a base branch
from
embeddedt:add/constant-pool-parsing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/main/java/cpw/mods/modlauncher/util/ClassConstantPoolParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/*** | ||
* This Class is derived from the ASM ClassReader | ||
* <p> | ||
* ASM: a very small and fast Java bytecode manipulation framework Copyright (c) 2000-2011 INRIA, France Telecom All | ||
* rights reserved. | ||
* <p> | ||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the | ||
* following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of | ||
* conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation and/or other materials provided with the | ||
* distribution. 3. Neither the name of the copyright holders nor the names of its contributors may be used to endorse | ||
* or promote products derived from this software without specific prior written permission. | ||
* <p> | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package cpw.mods.modlauncher.util; | ||
|
||
/** | ||
* Using this class to search for a (single) String reference is > 40 times faster than parsing a class with a ClassReader + | ||
* ClassNode while using way less RAM | ||
*/ | ||
public class ClassConstantPoolParser { | ||
|
||
private static final int UTF8 = 1; | ||
private static final int INT = 3; | ||
private static final int FLOAT = 4; | ||
private static final int LONG = 5; | ||
private static final int DOUBLE = 6; | ||
private static final int FIELD = 9; | ||
private static final int METH = 10; | ||
private static final int IMETH = 11; | ||
private static final int NAME_TYPE = 12; | ||
private static final int HANDLE = 15; | ||
private static final int INDY = 18; | ||
|
||
/** | ||
* Returns true if the constant pool of the class represented by this byte array contains one of the Strings we are looking | ||
* for | ||
*/ | ||
public static boolean constantPoolMatches(byte[][] stringsToSearch, byte[] basicClass) { | ||
if (stringsToSearch.length == 0) { | ||
return true; // empty list | ||
} | ||
if (basicClass == null || basicClass.length == 0) { | ||
return true; // assume empty classes match | ||
} | ||
|
||
// parses the constant pool | ||
int n = readUnsignedShort(8, basicClass); | ||
int index = 10; | ||
for (int i = 1; i < n; ++i) { | ||
int size; | ||
switch (basicClass[index]) { | ||
case FIELD: | ||
case METH: | ||
case IMETH: | ||
case INT: | ||
case FLOAT: | ||
case NAME_TYPE: | ||
case INDY: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You forgot |
||
size = 5; | ||
break; | ||
case LONG: | ||
case DOUBLE: | ||
size = 9; | ||
++i; | ||
break; | ||
case UTF8: | ||
final int strLen = readUnsignedShort(index + 1, basicClass); | ||
size = 3 + strLen; | ||
label: | ||
for (byte[] bytes : stringsToSearch) { | ||
if (strLen == bytes.length) { | ||
for (int j = index + 3; j < index + 3 + strLen; j++) { | ||
if (basicClass[j] != bytes[j - (index + 3)]) { | ||
break label; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
break; | ||
case HANDLE: | ||
size = 4; | ||
break; | ||
default: | ||
size = 3; | ||
break; | ||
} | ||
index += size; | ||
} | ||
return false; | ||
} | ||
|
||
private static short readShort(final int index, byte[] basicClass) { | ||
return (short) (((basicClass[index] & 0xFF) << 8) | (basicClass[index + 1] & 0xFF)); | ||
} | ||
|
||
private static int readUnsignedShort(final int index, byte[] basicClass) { | ||
return ((basicClass[index] & 0xFF) << 8) | (basicClass[index + 1] & 0xFF); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/cpw/mods/modlauncher/test/MockLaunchPluginService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cpw.mods.modlauncher.test; | ||
|
||
import cpw.mods.modlauncher.serviceapi.ILaunchPluginService; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.Type; | ||
import org.objectweb.asm.tree.ClassNode; | ||
import org.objectweb.asm.tree.FieldNode; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.EnumSet; | ||
|
||
public class MockLaunchPluginService implements ILaunchPluginService { | ||
@Override | ||
public String name() { | ||
return "testlaunchplugin"; | ||
} | ||
|
||
private static final EnumSet<Phase> YAY = EnumSet.of(Phase.BEFORE); | ||
|
||
@Override | ||
public EnumSet<Phase> handlesClass(Type classType, boolean isEmpty) { | ||
return YAY; | ||
} | ||
|
||
@Override | ||
public boolean processClass(Phase phase, ClassNode classNode, Type classType) { | ||
FieldNode fn = new FieldNode(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "testfield2", "Ljava/lang/String;", null, "BUTTER!"); | ||
classNode.fields.add(fn); | ||
return true; | ||
} | ||
|
||
// We'll test that filtering for the Ljava/lang/String; constant pool entry used by 'testfield' (which is injected by | ||
// the other mock transformer) works | ||
// Note: This assumes we run after that transformer | ||
|
||
private static final byte[][] FILTER = new byte[][] { | ||
"Ljava/lang/String;".getBytes(StandardCharsets.UTF_8) | ||
}; | ||
|
||
@Override | ||
public byte[][] constantsFilter(Type classType, String reason) { | ||
return FILTER; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a way to opt-out of this behavior in case it causes issues (for example due to forgetting to update the constant pool parser). Probably via an env variable or ML arg.