implements PR-O2.1
This commit is contained in:
+91
@@ -0,0 +1,91 @@
|
||||
package p.studio.compiler.backend.bytecode;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
public class BytecodeLinkPrecheckService {
|
||||
|
||||
public void validate(final BytecodeModule module) {
|
||||
final var input = module == null ? BytecodeModule.empty() : module;
|
||||
validateFunctionBounds(input);
|
||||
validateExports(input);
|
||||
validateHostcallIndices(input);
|
||||
}
|
||||
|
||||
private void validateFunctionBounds(final BytecodeModule module) {
|
||||
var previousEnd = 0;
|
||||
for (final var function : module.functions()) {
|
||||
if (function.codeOffset() < 0 || function.codeLen() < 0) {
|
||||
throw new BytecodeMarshalingException(
|
||||
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_FUNCTION_BOUNDS_INVALID,
|
||||
"negative function code bounds");
|
||||
}
|
||||
if (function.codeOffset() < previousEnd) {
|
||||
throw new BytecodeMarshalingException(
|
||||
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_FUNCTION_BOUNDS_INVALID,
|
||||
"function offsets must be monotonic");
|
||||
}
|
||||
final var end = function.codeOffset() + function.codeLen();
|
||||
if (end < function.codeOffset() || end > module.code().length) {
|
||||
throw new BytecodeMarshalingException(
|
||||
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_FUNCTION_BOUNDS_INVALID,
|
||||
"function code range exceeds module code length");
|
||||
}
|
||||
previousEnd = end;
|
||||
}
|
||||
}
|
||||
|
||||
private void validateExports(final BytecodeModule module) {
|
||||
for (final var export : module.exports()) {
|
||||
if (export.funcIdx() < 0 || export.funcIdx() >= module.functions().size()) {
|
||||
throw new BytecodeMarshalingException(
|
||||
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_EXPORT_FUNC_INDEX_INVALID,
|
||||
"export references invalid func_idx: " + export.funcIdx());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void validateHostcallIndices(final BytecodeModule module) {
|
||||
var pc = 0;
|
||||
while (pc < module.code().length) {
|
||||
if (pc + 2 > module.code().length) {
|
||||
throw new BytecodeMarshalingException(
|
||||
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_TRUNCATED_INSTRUCTION,
|
||||
"truncated opcode at pc=" + pc);
|
||||
}
|
||||
final var opcode = readU16(module.code(), pc);
|
||||
final var size = sizeForOpcode(opcode);
|
||||
if (pc + size > module.code().length) {
|
||||
throw new BytecodeMarshalingException(
|
||||
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_TRUNCATED_INSTRUCTION,
|
||||
"truncated instruction at pc=" + pc);
|
||||
}
|
||||
if (opcode == 0x71) {
|
||||
final var index = readU32(module.code(), pc + 2);
|
||||
if (index < 0 || index >= module.syscalls().size()) {
|
||||
throw new BytecodeMarshalingException(
|
||||
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_HOSTCALL_INDEX_INVALID,
|
||||
"hostcall index out of bounds: " + index);
|
||||
}
|
||||
}
|
||||
pc += size;
|
||||
}
|
||||
}
|
||||
|
||||
private int sizeForOpcode(final int opcode) {
|
||||
return switch (opcode) {
|
||||
case 0x50, 0x71, 0x72, 0x02, 0x03, 0x04, 0x10, 0x18, 0x40, 0x41, 0x42, 0x43, 0x56 -> 6;
|
||||
case 0x14, 0x15, 0x52, 0x54 -> 10;
|
||||
case 0x16 -> 3;
|
||||
default -> 2;
|
||||
};
|
||||
}
|
||||
|
||||
private int readU16(final byte[] code, final int offset) {
|
||||
return ByteBuffer.wrap(code, offset, 2).order(ByteOrder.LITTLE_ENDIAN).getShort() & 0xFFFF;
|
||||
}
|
||||
|
||||
private int readU32(final byte[] code, final int offset) {
|
||||
return ByteBuffer.wrap(code, offset, 4).order(ByteOrder.LITTLE_ENDIAN).getInt();
|
||||
}
|
||||
}
|
||||
+4
@@ -7,4 +7,8 @@ public enum BytecodeMarshalingErrorCode {
|
||||
MARSHAL_FORMAT_PC_SPAN_OUT_OF_BOUNDS,
|
||||
MARSHAL_LINKAGE_RAW_SYSCALL_IN_PRELOAD,
|
||||
MARSHAL_LINKAGE_HOST_ABI_MISMATCH,
|
||||
MARSHAL_VERIFY_PRECHECK_FUNCTION_BOUNDS_INVALID,
|
||||
MARSHAL_VERIFY_PRECHECK_EXPORT_FUNC_INDEX_INVALID,
|
||||
MARSHAL_VERIFY_PRECHECK_HOSTCALL_INDEX_INVALID,
|
||||
MARSHAL_VERIFY_PRECHECK_TRUNCATED_INSTRUCTION,
|
||||
}
|
||||
|
||||
+2
-1
@@ -21,7 +21,8 @@ public class BuilderPipelineService {
|
||||
new FrontendPhasePipelineStage(),
|
||||
new LowerToIRVMPipelineStage(),
|
||||
new OptimizeIRVMPipelineStage(),
|
||||
new EmitBytecodePipelineStage()
|
||||
new EmitBytecodePipelineStage(),
|
||||
new LinkBytecodePipelineStage()
|
||||
);
|
||||
INSTANCE = new BuilderPipelineService(stages);
|
||||
}
|
||||
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package p.studio.compiler.workspaces.stages;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import p.studio.compiler.backend.bytecode.BytecodeLinkPrecheckService;
|
||||
import p.studio.compiler.backend.bytecode.BytecodeMarshalingException;
|
||||
import p.studio.compiler.messages.BuildingIssueSink;
|
||||
import p.studio.compiler.models.BuilderPipelineContext;
|
||||
import p.studio.compiler.workspaces.PipelineStage;
|
||||
import p.studio.utilities.logs.LogAggregator;
|
||||
|
||||
@Slf4j
|
||||
public class LinkBytecodePipelineStage implements PipelineStage {
|
||||
private final BytecodeLinkPrecheckService linkPrecheckService;
|
||||
|
||||
public LinkBytecodePipelineStage() {
|
||||
this(new BytecodeLinkPrecheckService());
|
||||
}
|
||||
|
||||
LinkBytecodePipelineStage(final BytecodeLinkPrecheckService linkPrecheckService) {
|
||||
this.linkPrecheckService = linkPrecheckService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BuildingIssueSink run(BuilderPipelineContext ctx, LogAggregator logs) {
|
||||
if (ctx.bytecodeModule == null) {
|
||||
return BuildingIssueSink.empty()
|
||||
.report(builder -> builder
|
||||
.error(true)
|
||||
.phase("BACKEND_LINK_PRECHECK")
|
||||
.code("MARSHAL_FORMAT_STAGE_INPUT_MISSING")
|
||||
.message("[BUILD]: bytecode module is missing before LinkBytecode stage"));
|
||||
}
|
||||
|
||||
try {
|
||||
linkPrecheckService.validate(ctx.bytecodeModule);
|
||||
} catch (BytecodeMarshalingException e) {
|
||||
return BuildingIssueSink.empty()
|
||||
.report(builder -> builder
|
||||
.error(true)
|
||||
.phase("BACKEND_LINK_PRECHECK")
|
||||
.code(e.code().name())
|
||||
.message("[BUILD]: bytecode link precheck failed: " + e.getMessage())
|
||||
.exception(e));
|
||||
}
|
||||
|
||||
return BuildingIssueSink.empty();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user