Skip to content

Commit

Permalink
Improved PMD config
Browse files Browse the repository at this point in the history
  • Loading branch information
Ledmington committed Jan 10, 2025
1 parent e7dfdcc commit b192165
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 70 deletions.
2 changes: 1 addition & 1 deletion elf/src/main/java/com/ledmington/elf/ELF.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public int getProgramHeaderTableLength() {
}

@Override
public PHTEntry getProgramHeader(int idx) {
public PHTEntry getProgramHeader(final int idx) {
return programHeaderTable[idx];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ public interface ProgramHeaderTable {
* @param idx The index of the program header to return.
* @return The i-th program header.
*/
PHTEntry getProgramHeader(final int idx);
PHTEntry getProgramHeader(int idx);
}
2 changes: 1 addition & 1 deletion elf/src/main/java/com/ledmington/elf/SectionTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ public interface SectionTable {
* @param idx The index of the section to return.
* @return The i-th section.
*/
Section getSection(final int idx);
Section getSection(int idx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static NoteSectionEntry[] loadNoteSectionEntries(final ReadOnlyByteBuffer b, fin
* @param idx The index of the entry.
* @return The entry at teh given index.
*/
NoteSectionEntry getEntry(final int idx);
NoteSectionEntry getEntry(int idx);

@Override
default byte[] getLoadableContent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface SymbolTable extends Section {
* @param idx The index of the entry to return.
* @return The i-th entry in the symbol table.
*/
SymbolTableEntry getSymbolTableEntry(final int idx);
SymbolTableEntry getSymbolTableEntry(int idx);

/**
* Looks for a symbol with the given value and returns it.
Expand Down
3 changes: 2 additions & 1 deletion emu-cli/src/main/java/com/ledmington/emu/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public static void main(final String[] args) {
int k = 0;
long bytes = 0L;
while (k < s.length() && Character.isDigit(s.charAt(k))) {
bytes = bytes * 10L + (s.charAt(k) - '0');
final long idx = (long) (s.charAt(k) - '0');
bytes = bytes * 10L + idx;
k++;
}

Expand Down
6 changes: 3 additions & 3 deletions emu-gui/src/main/java/com/ledmington/view/EmulatorView.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Map;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -71,8 +71,8 @@ public final class EmulatorView extends Stage {
private final Button runBtn = new Button();
private final TextArea codeArea = new TextArea();
private final TextArea memoryArea = new TextArea();
private final Map<Register64, Label> regLabels = new HashMap<>();
private final Map<Register16, Label> segLabels = new HashMap<>();
private final Map<Register64, Label> regLabels = new EnumMap<>(Register64.class);
private final Map<Register16, Label> segLabels = new EnumMap<>(Register16.class);
private final Label rflagsLabel;

public EmulatorView() {
Expand Down
20 changes: 10 additions & 10 deletions emu/src/test/java/com/ledmington/emu/TestX86RegisterFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class TestX86RegisterFile {
private static final RandomGenerator rng =
RandomGeneratorFactory.getDefault().create(System.nanoTime());
private X86RegisterFile regFile;
private static final Register8[] all8BitRegisters = new Register8[] {
private static final Register8[] all8BitRegisters = {
Register8.AL,
Register8.AH,
Register8.BL,
Expand All @@ -63,7 +63,7 @@ final class TestX86RegisterFile {
Register8.SIL,
Register8.DIL
};
private static final Register16[] all16BitRegisters = new Register16[] {
private static final Register16[] all16BitRegisters = {
Register16.AX,
Register16.BX,
Register16.CX,
Expand All @@ -87,7 +87,7 @@ final class TestX86RegisterFile {
Register16.GS,
Register16.SS
};
private static final Register32[] all32BitRegisters = new Register32[] {
private static final Register32[] all32BitRegisters = {
Register32.EAX,
Register32.EBX,
Register32.ECX,
Expand All @@ -106,7 +106,7 @@ final class TestX86RegisterFile {
Register32.R15D,
Register32.EIP
};
private static final Register64[] all64BitRegisters = new Register64[] {
private static final Register64[] all64BitRegisters = {
Register64.RAX,
Register64.RBX,
Register64.RCX,
Expand All @@ -131,7 +131,7 @@ void setup() {
regFile = new X86RegisterFile();
}

static Stream<Arguments> all8BitsRegisters() {
private static Stream<Arguments> all8BitsRegisters() {
return Arrays.stream(all8BitRegisters).map(Arguments::of);
}

Expand Down Expand Up @@ -172,7 +172,7 @@ void setToValueShouldNotChangeOtherRegisters(final Register8 r) {
}
}

static Stream<Arguments> all16BitsRegisters() {
private static Stream<Arguments> all16BitsRegisters() {
return Arrays.stream(all16BitRegisters).map(Arguments::of);
}

Expand All @@ -188,7 +188,7 @@ void initiallyAllZero(final Register16 r) {
@ParameterizedTest
@MethodSource("all16BitsRegisters")
void setToValue(final Register16 r) {
final short x = BitUtils.asShort(rng.nextInt(1, 65536));
final short x = BitUtils.asShort(rng.nextInt(1, 65_536));
regFile.set(r, x);
assertEquals(
x,
Expand All @@ -199,7 +199,7 @@ void setToValue(final Register16 r) {
@ParameterizedTest
@MethodSource("all16BitsRegisters")
void setToValueShouldNotChangeOtherRegisters(final Register16 r) {
final short x = BitUtils.asShort(rng.nextInt(1, 65536));
final short x = BitUtils.asShort(rng.nextInt(1, 65_536));
regFile.set(r, x);

for (final Register16 other : all16BitRegisters) {
Expand All @@ -213,7 +213,7 @@ void setToValueShouldNotChangeOtherRegisters(final Register16 r) {
}
}

static Stream<Arguments> all32BitsRegisters() {
private static Stream<Arguments> all32BitsRegisters() {
return Arrays.stream(all32BitRegisters).map(Arguments::of);
}

Expand Down Expand Up @@ -254,7 +254,7 @@ void setToValueShouldNotChangeOtherRegisters(final Register32 r) {
}
}

static Stream<Arguments> all64BitsRegisters() {
private static Stream<Arguments> all64BitsRegisters() {
return Arrays.stream(all64BitRegisters).map(Arguments::of);
}

Expand Down
2 changes: 1 addition & 1 deletion id/src/jmh/java/com/ledmington/id/Decoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Decoding {
}

// Bytes of the instruction 'cmp WORD PTR [r9+rcx*4+0x12345678],0xbeef'
private final byte[] instructionBytes = new byte[] {
private final byte[] instructionBytes = {
(byte) 0x66,
(byte) 0x41,
(byte) 0x81,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,7 @@ private Prefixes parsePrefixes() {
// FIXME: is there a better way to do this?
// (technically there is no limit to the number of prefixes an instruction can have)
while (true) {
byte x = b.read1();
final byte x = b.read1();

if (isLegacyPrefixGroup1(x)) {
p1 = Optional.of(InstructionPrefix.fromByte(x));
Expand Down Expand Up @@ -1978,7 +1978,7 @@ private Instruction parseMOV(
final boolean hasOperandSizeOverridePrefix = pref.hasOperandSizeOverridePrefix();
final RexPrefix rexPrefix = pref.rex();
final byte rm = modrm.rm();
Operand operand1;
final Operand operand1;

// Table at page 530
final byte mod = modrm.mod();
Expand Down Expand Up @@ -2015,7 +2015,7 @@ private IndirectOperandBuilder parseIndirectOperand(final Prefixes pref, final M
final RexPrefix rexPrefix = pref.rex();
final boolean hasAddressSizeOverridePrefix = pref.hasAddressSizeOverridePrefix();
final IndirectOperandBuilder iob = IndirectOperand.builder();
SIB sib;
final SIB sib;
if (modrm.mod() != MODRM_MOD_NO_DISP && modrm.rm() == 0b00000100) {
// SIB needed
sib = sib();
Expand Down
6 changes: 3 additions & 3 deletions id/src/test/java/FormatTestFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ private static List<String> readAllLines(final String filepath) {
}

tc.stream()
.sorted((a, b) -> ((a.mnemonic().equals(b.mnemonic()))
? (a.hex().compareTo(b.hex()))
: (a.mnemonic().compareTo(b.mnemonic()))))
.sorted((a, b) -> a.mnemonic().equals(b.mnemonic())
? a.hex().compareTo(b.hex())
: a.mnemonic().compareTo(b.mnemonic()))
.forEach(e -> allLines.add(String.format(fmt + " | %s", e.mnemonic(), e.hex())));
}

Expand Down
8 changes: 4 additions & 4 deletions id/src/test/java/com/ledmington/cpu/x86/TestREXPrefix.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ static Stream<Arguments> correctPrefixes() {
for (final boolean b : v) {
final byte prefix = BitUtils.or(
(byte) 0x40,
(w ? (byte) 0x08 : 0),
(r ? (byte) 0x04 : 0),
(x ? (byte) 0x02 : 0),
(b ? (byte) 0x01 : 0));
w ? (byte) 0x08 : 0,
r ? (byte) 0x04 : 0,
x ? (byte) 0x02 : 0,
b ? (byte) 0x01 : 0);

args.add(Arguments.of(w, r, x, b, prefix));
}
Expand Down
6 changes: 3 additions & 3 deletions mem/src/main/java/com/ledmington/mem/Memory.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public interface Memory {
* @param address The address to read from.
* @return The byte word contained at the given address.
*/
byte read(final long address);
byte read(long address);

/**
* Writes the given byte word at the given address, overwriting any value previously stored at that location.
*
* @param address The address to write at.
* @param value The value to write.
*/
void write(final long address, final byte value);
void write(long address, byte value);

/**
* Checks whether the given address is initialized or not. A memory address is said to be initialized if it has been
Expand All @@ -47,5 +47,5 @@ public interface Memory {
* @param address The address to be checked.
* @return True if it is initialized, false otherwise.
*/
boolean isInitialized(final long address);
boolean isInitialized(long address);
}
62 changes: 37 additions & 25 deletions mem/src/main/java/com/ledmington/mem/MemoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
public final class MemoryController implements Memory {

private final Memory mem;
private final IntervalArray canRead = new IntervalArray();
private final IntervalArray canWrite = new IntervalArray();
private final IntervalArray canExecute = new IntervalArray();
private final IntervalArray readableAddresses = new IntervalArray();
private final IntervalArray writableAddresses = new IntervalArray();
private final IntervalArray executableAddresses = new IntervalArray();

// TODO: this seems like a poor design choice
private final boolean breakOnWrongPermissions;
Expand Down Expand Up @@ -69,6 +69,18 @@ public MemoryController(final Memory memory) {
this(memory, true, true);
}

private boolean canRead(final long address) {
return readableAddresses.get(address);
}

private boolean canWrite(final long address) {
return writableAddresses.get(address);
}

private boolean canExecute(final long address) {
return executableAddresses.get(address);
}

private String reportIllegalAccess(final String message, final long address, final int length) {
final String reset = "\u001B[0m";
final String bold = "\u001B[1m";
Expand All @@ -84,7 +96,7 @@ private String reportIllegalAccess(final String message, final long address, fin
final long bytesPerLine = 16;

// The start of the given range must be in the middle 16-bytes aligned line
final long startAddress = (address / bytesPerLine) * bytesPerLine - bytesPerLine * linesAround;
final long startAddress = address / bytesPerLine * bytesPerLine - bytesPerLine * linesAround;

final StringBuilder sb = new StringBuilder(128);

Expand Down Expand Up @@ -128,9 +140,9 @@ private String reportIllegalAccess(final String message, final long address, fin
if (x >= address && x < address + length) {
sb.append(bold);
}
final boolean r = canRead.get(x);
final boolean w = canWrite.get(x);
final boolean e = canExecute.get(x);
final boolean r = canRead(x);
final boolean w = canWrite(x);
final boolean e = canExecute(x);
if (r && !w && !e) {
sb.append(red);
}
Expand Down Expand Up @@ -256,27 +268,27 @@ public void setPermissions(
}

if (readable) {
canRead.set(startBlockAddress, endBlockAddress);
readableAddresses.set(startBlockAddress, endBlockAddress);
} else {
canRead.reset(startBlockAddress, endBlockAddress);
readableAddresses.reset(startBlockAddress, endBlockAddress);
}

if (writeable) {
canWrite.set(startBlockAddress, endBlockAddress);
writableAddresses.set(startBlockAddress, endBlockAddress);
} else {
canWrite.reset(startBlockAddress, endBlockAddress);
writableAddresses.reset(startBlockAddress, endBlockAddress);
}

if (executable) {
canExecute.set(startBlockAddress, endBlockAddress);
executableAddresses.set(startBlockAddress, endBlockAddress);
} else {
canExecute.reset(startBlockAddress, endBlockAddress);
executableAddresses.reset(startBlockAddress, endBlockAddress);
}
}

private void checkRead(final long address, final int length) {
for (int i = 0; i < length; i++) {
if (breakOnWrongPermissions && !canRead.get(address + i)) {
if (breakOnWrongPermissions && !canRead(address + i)) {
reportIllegalRead(address, length);
}
if (breakWhenReadingUninitializedMemory && !isInitialized(address + i)) {
Expand Down Expand Up @@ -332,7 +344,7 @@ public long read8(final long address) {

private void checkExecute(final long address, final int length) {
for (int i = 0; i < length; i++) {
if (breakOnWrongPermissions && !canExecute.get(address + i)) {
if (breakOnWrongPermissions && !canExecute(address + i)) {
reportIllegalExecution(address, length);
}
if (breakWhenReadingUninitializedMemory && !isInitialized(address + i)) {
Expand All @@ -354,7 +366,7 @@ public byte readCode(final long address) {

private void checkWrite(final long address, final int length) {
for (int i = 0; i < length; i++) {
if (breakOnWrongPermissions && !canWrite.get(address + i)) {
if (breakOnWrongPermissions && !canWrite(address + i)) {
reportIllegalWrite(address, length);
}
}
Expand Down Expand Up @@ -429,18 +441,18 @@ public void initialize(final long address, final byte value) {
@Override
public String toString() {
return "MemoryController(mem=" + mem + ";canRead="
+ canRead + ";canWrite="
+ canWrite + ";canExecute="
+ canExecute + ')';
+ readableAddresses + ";canWrite="
+ writableAddresses + ";canExecute="
+ executableAddresses + ')';
}

@Override
public int hashCode() {
int h = 17;
h = 31 * h + mem.hashCode();
h = 31 * h + canRead.hashCode();
h = 31 * h + canWrite.hashCode();
h = 31 * h + canExecute.hashCode();
h = 31 * h + readableAddresses.hashCode();
h = 31 * h + writableAddresses.hashCode();
h = 31 * h + executableAddresses.hashCode();
return h;
}

Expand All @@ -456,8 +468,8 @@ public boolean equals(final Object other) {
return false;
}
return this.mem.equals(m)
&& this.canRead.equals(m.canRead)
&& this.canWrite.equals(m.canWrite)
&& this.canExecute.equals(m.canExecute);
&& this.readableAddresses.equals(m.readableAddresses)
&& this.writableAddresses.equals(m.writableAddresses)
&& this.executableAddresses.equals(m.executableAddresses);
}
}
Loading

0 comments on commit b192165

Please sign in to comment.