Add Int*Arrays.

This commit is contained in:
Andy Nguyen 2024-12-06 09:15:57 +01:00
parent 639bda1537
commit 20c77c4f47
14 changed files with 141 additions and 154 deletions

2
.gitignore vendored
View File

@ -7,3 +7,5 @@ disc/BDMV/index.bdmv
disc/BDMV/MovieObject.bdmv disc/BDMV/MovieObject.bdmv
disc/BDMV/JAR/00000.jar disc/BDMV/JAR/00000.jar
disc/BDMV/BDJO/00000.bdjo disc/BDMV/BDJO/00000.bdjo
META-INF/
bd-jb.iml

View File

@ -17,7 +17,6 @@ EXPLOIT_CLASSES = \
$(SRC)/com/bdjb/api/KernelAPI.java \ $(SRC)/com/bdjb/api/KernelAPI.java \
$(SRC)/com/bdjb/api/Buffer.java \ $(SRC)/com/bdjb/api/Buffer.java \
$(SRC)/com/bdjb/api/Text.java \ $(SRC)/com/bdjb/api/Text.java \
$(SRC)/com/bdjb/api/AbstractInt.java \
$(SRC)/com/bdjb/api/Int8.java \ $(SRC)/com/bdjb/api/Int8.java \
$(SRC)/com/bdjb/api/Int16.java \ $(SRC)/com/bdjb/api/Int16.java \
$(SRC)/com/bdjb/api/Int32.java \ $(SRC)/com/bdjb/api/Int32.java \

View File

@ -440,6 +440,14 @@ public final class API {
return unsafe.allocateMemory(size); return unsafe.allocateMemory(size);
} }
public long calloc(long number, long size) {
long p = malloc(number * size);
if (p != 0) {
memset(p, 0, number * size);
}
return p;
}
public long realloc(long ptr, long size) { public long realloc(long ptr, long size) {
return unsafe.reallocateMemory(ptr, size); return unsafe.reallocateMemory(ptr, size);
} }

View File

@ -1,57 +0,0 @@
/*
* Copyright (C) 2021-2024 Andy Nguyen
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package com.bdjb.api;
abstract class AbstractInt extends Buffer {
private final int[] dimensions;
private final int elementSize;
protected AbstractInt(int[] dimensions, int elementSize) {
super(size(dimensions, elementSize));
this.dimensions = dimensions;
this.elementSize = elementSize;
}
protected AbstractInt(long address, int[] dimensions, int elementSize) {
super(address, size(dimensions, elementSize));
this.dimensions = dimensions;
this.elementSize = elementSize;
}
protected AbstractInt(long address, int elementSize) {
this(address, new int[] {1}, elementSize);
}
protected AbstractInt(int elementSize) {
this(new int[] {1}, elementSize);
}
static int size(int[] dimensions, int elementSize) {
assert (dimensions.length > 0);
int size = 1;
for (int i = 0; i < dimensions.length; i++) {
size *= dimensions[i];
}
size *= elementSize;
return size;
}
public int offset(int[] indices) {
assert (indices.length == dimensions.length);
int offset = 0;
int stride = 1;
for (int i = indices.length - 1; i >= 0; i--) {
offset += stride * indices[i];
stride *= dimensions[i];
}
offset *= elementSize;
checkOffset(offset, elementSize);
return offset;
}
}

View File

@ -22,25 +22,14 @@ public class Buffer {
private final int size; private final int size;
private final boolean allocated;
public Buffer(int size) { public Buffer(int size) {
this.address = api.malloc(size); this.address = api.calloc(1, size);
this.size = size; this.size = size;
this.allocated = true;
}
public Buffer(long address, int size) {
this.address = address;
this.size = size;
this.allocated = false;
} }
public void finalize() { public void finalize() {
if (allocated) {
api.free(address); api.free(address);
} }
}
public long address() { public long address() {
return address; return address;

View File

@ -7,23 +7,16 @@
package com.bdjb.api; package com.bdjb.api;
public final class Int16 extends AbstractInt { public final class Int16 extends Buffer {
public static final int SIZE = 2; public static final int SIZE = 2;
public Int16() { public Int16() {
super(SIZE); super(SIZE);
} }
public Int16(long address) { public Int16(short value) {
super(address, SIZE); this();
} set(value);
public Int16(int[] dimensions) {
super(dimensions, SIZE);
}
public Int16(long address, int[] dimensions) {
super(address, dimensions, SIZE);
} }
public short get() { public short get() {
@ -33,12 +26,4 @@ public final class Int16 extends AbstractInt {
public void set(short value) { public void set(short value) {
putShort(0x00, value); putShort(0x00, value);
} }
public short get(int[] indices) {
return getShort(offset(indices));
}
public void set(int[] indices, short value) {
putShort(offset(indices), value);
}
} }

View File

@ -0,0 +1,22 @@
/*
* Copyright (C) 2021-2024 Andy Nguyen
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package com.bdjb.api;
public final class Int16Array extends Buffer {
public Int16Array(int length) {
super(Int16.SIZE);
}
public short get(int index) {
return getShort(index * Int16.SIZE);
}
public void set(int index, short value) {
putShort(index * Int16.SIZE, value);
}
}

View File

@ -7,23 +7,16 @@
package com.bdjb.api; package com.bdjb.api;
public final class Int32 extends AbstractInt { public final class Int32 extends Buffer {
public static final int SIZE = 4; public static final int SIZE = 4;
public Int32() { public Int32() {
super(SIZE); super(SIZE);
} }
public Int32(long address) { public Int32(int value) {
super(address, SIZE); this();
} set(value);
public Int32(int[] dimensions) {
super(dimensions, SIZE);
}
public Int32(long address, int[] dimensions) {
super(address, dimensions, SIZE);
} }
public int get() { public int get() {
@ -33,12 +26,4 @@ public final class Int32 extends AbstractInt {
public void set(int value) { public void set(int value) {
putInt(0x00, value); putInt(0x00, value);
} }
public int get(int[] indices) {
return getInt(offset(indices));
}
public void set(int[] indices, int value) {
putInt(offset(indices), value);
}
} }

View File

@ -0,0 +1,22 @@
/*
* Copyright (C) 2021-2024 Andy Nguyen
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package com.bdjb.api;
public final class Int32Array extends Buffer {
public Int32Array(int length) {
super(Int32.SIZE);
}
public int get(int index) {
return getInt(index * Int32.SIZE);
}
public void set(int index, int value) {
putInt(index * Int32.SIZE, value);
}
}

View File

@ -7,23 +7,16 @@
package com.bdjb.api; package com.bdjb.api;
public final class Int64 extends AbstractInt { public final class Int64 extends Buffer {
public static final int SIZE = 8; public static final int SIZE = 8;
public Int64() { public Int64() {
super(SIZE); super(SIZE);
} }
public Int64(long address) { public Int64(long value) {
super(address, SIZE); this();
} set(value);
public Int64(int[] dimensions) {
super(dimensions, SIZE);
}
public Int64(long address, int[] dimensions) {
super(address, dimensions, SIZE);
} }
public long get() { public long get() {
@ -33,12 +26,4 @@ public final class Int64 extends AbstractInt {
public void set(long value) { public void set(long value) {
putLong(0x00, value); putLong(0x00, value);
} }
public long get(int[] indices) {
return getLong(offset(indices));
}
public void set(int[] indices, long value) {
putLong(offset(indices), value);
}
} }

View File

@ -0,0 +1,22 @@
/*
* Copyright (C) 2021-2024 Andy Nguyen
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package com.bdjb.api;
public final class Int64Array extends Buffer {
public Int64Array(int length) {
super(Int64.SIZE);
}
public long get(int index) {
return getLong(index * Int64.SIZE);
}
public void set(int index, long value) {
putLong(index * Int64.SIZE, value);
}
}

View File

@ -7,23 +7,16 @@
package com.bdjb.api; package com.bdjb.api;
public final class Int8 extends AbstractInt { public final class Int8 extends Buffer {
public static final int SIZE = 1; public static final int SIZE = 1;
public Int8() { public Int8() {
super(SIZE); super(SIZE);
} }
public Int8(long address) { public Int8(byte value) {
super(address, SIZE); this();
} set(value);
public Int8(int[] dimensions) {
super(dimensions, SIZE);
}
public Int8(long address, int[] dimensions) {
super(address, dimensions, SIZE);
} }
public byte get() { public byte get() {
@ -33,12 +26,4 @@ public final class Int8 extends AbstractInt {
public void set(byte value) { public void set(byte value) {
putByte(0x00, value); putByte(0x00, value);
} }
public byte get(int[] indices) {
return getByte(offset(indices));
}
public void set(int[] indices, byte value) {
putByte(offset(indices), value);
}
} }

View File

@ -0,0 +1,22 @@
/*
* Copyright (C) 2021-2024 Andy Nguyen
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package com.bdjb.api;
public final class Int8Array extends Buffer {
public Int8Array(int length) {
super(Int8.SIZE);
}
public byte get(int index) {
return getByte(index * Int8.SIZE);
}
public void set(int index, byte value) {
putByte(index * Int8.SIZE, value);
}
}

View File

@ -40,6 +40,8 @@ public class KernelAPI {
private long fcntl; private long fcntl;
private long close; private long close;
private long kaslrOffset;
private int masterRpipeFd; private int masterRpipeFd;
private int masterWpipeFd; private int masterWpipeFd;
private int victimRpipeFd; private int victimRpipeFd;
@ -77,16 +79,16 @@ public class KernelAPI {
} }
private void initPipes() { private void initPipes() {
Int32 masterPipeFd = new Int32(new int[] {2}); Int32Array masterPipeFd = new Int32Array(2);
Int32 victimPipeFd = new Int32(new int[] {2}); Int32Array victimPipeFd = new Int32Array(2);
pipe(masterPipeFd); pipe(masterPipeFd);
pipe(victimPipeFd); pipe(victimPipeFd);
masterRpipeFd = masterPipeFd.get(new int[] {0}); masterRpipeFd = masterPipeFd.get(0);
masterWpipeFd = masterPipeFd.get(new int[] {1}); masterWpipeFd = masterPipeFd.get(1);
victimRpipeFd = victimPipeFd.get(new int[] {0}); victimRpipeFd = victimPipeFd.get(0);
victimWpipeFd = victimPipeFd.get(new int[] {1}); victimWpipeFd = victimPipeFd.get(1);
fcntl(masterRpipeFd, F_SETFL, O_NONBLOCK); fcntl(masterRpipeFd, F_SETFL, O_NONBLOCK);
fcntl(masterWpipeFd, F_SETFL, O_NONBLOCK); fcntl(masterWpipeFd, F_SETFL, O_NONBLOCK);
@ -94,7 +96,7 @@ public class KernelAPI {
fcntl(victimWpipeFd, F_SETFL, O_NONBLOCK); fcntl(victimWpipeFd, F_SETFL, O_NONBLOCK);
} }
private int pipe(Int32 fildes) { private int pipe(Int32Array fildes) {
return (int) api.call(pipe, fildes != null ? fildes.address() : 0); return (int) api.call(pipe, fildes != null ? fildes.address() : 0);
} }
@ -181,7 +183,23 @@ public class KernelAPI {
return masterRpipeFd; return masterRpipeFd;
} }
public int getMasterWpipeFd() {
return masterWpipeFd;
}
public int getVictimRpipeFd() { public int getVictimRpipeFd() {
return victimRpipeFd; return victimRpipeFd;
} }
public int getVictimWpipeFd() {
return victimWpipeFd;
}
public long getKaslrOffset() {
return kaslrOffset;
}
public void setKaslrOffset(long offset) {
kaslrOffset = offset;
}
} }