Skip to content

Commit

Permalink
Fix and test line number remapper (#1097)
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 authored Apr 15, 2024
1 parent 02af089 commit 1b1168d
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ public void processEntryAsync(Path file, Path dst) throws IOException {
}
}

String fileName = file.getFileName().toString();
String fileName = file.toAbsolutePath().toString();

if (fileName.endsWith(".class")) {
String idx = fileName.substring(0, fileName.length() - 6);

LOGGER.debug("Remapping line numbers for class: " + idx);
// Strip the leading slash and the .class extension
String idx = fileName.substring(1, fileName.length() - 6);

int dollarPos = idx.indexOf('$'); //This makes the assumption that only Java classes are to be remapped.

Expand All @@ -74,6 +73,8 @@ public void processEntryAsync(Path file, Path dst) throws IOException {
}

if (lineNumbers.lineMap().containsKey(idx)) {
LOGGER.debug("Remapping line numbers for class: {}", idx);

try (InputStream is = Files.newInputStream(file)) {
ClassReader reader = new ClassReader(is);
ClassWriter writer = new ClassWriter(0);
Expand All @@ -82,6 +83,8 @@ public void processEntryAsync(Path file, Path dst) throws IOException {
Files.write(dst, writer.toByteArray());
return;
}
} else {
LOGGER.debug("No linemap found for: {}", idx);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class DebugLineNumbersTest extends Specification implements GradleProjectTestTra
def result = it.get()
println("Breakpoint triggered: ${result.location()}")
}

println("All breakpoints triggered")
} finally {
// Close the debugger and target process
debugger.close()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2024 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package net.fabricmc.loom.test.unit

import java.nio.file.Files

import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.Label
import org.objectweb.asm.MethodVisitor
import spock.lang.Specification

import net.fabricmc.loom.decompilers.ClassLineNumbers
import net.fabricmc.loom.decompilers.LineNumberRemapper
import net.fabricmc.loom.test.util.ZipTestUtils
import net.fabricmc.loom.util.Constants
import net.fabricmc.loom.util.ZipUtils

class LineNumberRemapperTests extends Specification {
def "remapLinenumbers"() {
given:
def className = LineNumberSource.class.name.replace('.', '/')
def input = ZipTestUtils.createZipFromBytes([(className + ".class"): getClassBytes(LineNumberSource.class)])

// + 10 to each line number
def entry = new ClassLineNumbers.Entry(className, 30, 40, [
27: 37,
29: 39,
30: 40
])
def lineNumbers = new ClassLineNumbers([(className): entry])

def outputJar = Files.createTempDirectory("loom").resolve("output.jar")

when:
def remapper = new LineNumberRemapper(lineNumbers)
remapper.process(input, outputJar)

def unpacked = ZipUtils.unpack(outputJar, className + ".class")

then:
readLineNumbers(getClassBytes(LineNumberSource.class)) == [27, 29, 30]
readLineNumbers(unpacked) == [37, 39, 40]
}

static byte[] getClassBytes(Class<?> clazz) {
return clazz.classLoader.getResourceAsStream(clazz.name.replace('.', '/') + ".class").withCloseable {
it.bytes
}
}

static List<Integer> readLineNumbers(byte[] clazzBytes) {
def reader = new ClassReader(clazzBytes)
def visitor = new LineNumberCollectorClassVisitor()
reader.accept(visitor, 0)
return visitor.lineNumbers
}

private static class LineNumberCollectorClassVisitor extends ClassVisitor {
final List<Integer> lineNumbers = []

protected LineNumberCollectorClassVisitor() {
super(Constants.ASM_VERSION)
}

@Override
MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
return new LineNumberCollectorMethodVisitor(super.visitMethod(access, name, descriptor, signature, exceptions), lineNumbers)
}
}

private static class LineNumberCollectorMethodVisitor extends MethodVisitor {
final List<Integer> lineNumbers

protected LineNumberCollectorMethodVisitor(MethodVisitor methodVisitor, List<Integer> lineNumbers) {
super(Constants.ASM_VERSION, methodVisitor)
this.lineNumbers = lineNumbers
}

@Override
void visitLineNumber(int line, Label start) {
super.visitLineNumber(line, start)
lineNumbers.add(line)
}
}
}
11 changes: 10 additions & 1 deletion src/test/groovy/net/fabricmc/loom/test/util/ZipTestUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ import net.fabricmc.loom.util.FileSystemUtil

class ZipTestUtils {
static Path createZip(Map<String, String> entries) {
return createZipFromBytes(entries.collectEntries { k, v ->
[
k,
v.getBytes(StandardCharsets.UTF_8)
]
})
}

static Path createZipFromBytes(Map<String, byte[]> entries) {
def file = Files.createTempFile("loom-test", ".zip")
Files.delete(file)

Expand All @@ -42,7 +51,7 @@ class ZipTestUtils {
def fsPath = zip.getPath(path)
def fsPathParent = fsPath.getParent()
if (fsPathParent != null) Files.createDirectories(fsPathParent)
Files.writeString(fsPath, value, StandardCharsets.UTF_8)
Files.write(fsPath, value)
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/test/java/net/fabricmc/loom/test/unit/LineNumberSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2024 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package net.fabricmc.loom.test.unit;

public class LineNumberSource {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

0 comments on commit 1b1168d

Please sign in to comment.