Skip to content

Commit

Permalink
svm: adjust sbpf version naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexicon226 committed Jan 21, 2025
1 parent 77518dd commit 7781166
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 78 deletions.
14 changes: 7 additions & 7 deletions src/svm/elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub const Elf = struct {
headers: Headers,
data: Data,
entry_pc: u64,
version: sbpf.SBPFVersion,
version: sbpf.Version,
function_registry: Registry(u64),
config: Config,

Expand Down Expand Up @@ -231,10 +231,10 @@ pub const Elf = struct {
const offset = headers.header.e_entry -| text_section.sh_addr;
const entry_pc = try std.math.divExact(u64, offset, 8);

const sbpf_version: sbpf.SBPFVersion = if (headers.header.e_flags == sbpf.EF_SBPF_V2)
.v2
const sbpf_version: sbpf.Version = if (headers.header.e_flags == sbpf.EF_SBPF_v1)
.v1
else
.v1;
.v0;

if (@intFromEnum(sbpf_version) < @intFromEnum(config.minimum_version))
return error.VersionUnsupported;
Expand Down Expand Up @@ -540,7 +540,7 @@ pub const Elf = struct {
const in_text_section = self.inRangeOfShdr(
text_section_index,
r_offset,
) or version == .v1;
) or version == .v0;
const imm_offset = if (in_text_section) r_offset +| 4 else r_offset;

const addr_slice = try safeSlice(self.bytes, imm_offset, 4);
Expand All @@ -552,7 +552,7 @@ pub const Elf = struct {
addr +|= memory.PROGRAM_START;
}

if (in_text_section or version == .v1) {
if (in_text_section or version == .v0) {
{
const imm_low_offset = imm_offset;
const imm_slice = try safeSlice(self.bytes, imm_low_offset, 4);
Expand Down Expand Up @@ -616,7 +616,7 @@ pub const Elf = struct {
}
} else {
const address: u64 = switch (version) {
.v1 => addr: {
.v0 => addr: {
const addr_slice = try safeSlice(self.bytes, imm_offset, 4);
const address = std.mem.readInt(u32, addr_slice[0..4], .little);
break :addr memory.PROGRAM_START +| address;
Expand Down
6 changes: 3 additions & 3 deletions src/svm/executable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Vm = @import("vm.zig").Vm;
pub const Executable = struct {
bytes: []const u8,
instructions: []align(1) const sbpf.Instruction,
version: sbpf.SBPFVersion,
version: sbpf.Version,
entry_pc: u64,
from_elf: bool,
ro_section: Section,
Expand Down Expand Up @@ -64,7 +64,7 @@ pub const Executable = struct {
pub fn fromTextBytes(
allocator: std.mem.Allocator,
source: []const u8,
version: sbpf.SBPFVersion,
version: sbpf.Version,
registry: *Registry(u64),
config: Config,
) !Executable {
Expand Down Expand Up @@ -549,7 +549,7 @@ pub const BuiltinProgram = struct {
pub const Config = struct {
optimize_rodata: bool = true,
reject_broken_elfs: bool = false,
minimum_version: sbpf.SBPFVersion = .v2,
minimum_version: sbpf.Version = .v1,
stack_frame_size: u64 = 4096,
max_call_depth: u64 = 64,

Expand Down
2 changes: 1 addition & 1 deletion src/svm/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn main() !void {
}

const config: Config = .{
.minimum_version = if (assemble) .v2 else .v1,
.minimum_version = if (assemble) .v1 else .v0,
};
var executable = if (assemble)
try Executable.fromAsm(allocator, bytes, config)
Expand Down
12 changes: 6 additions & 6 deletions src/svm/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub const MemoryMap = union(enum) {
aligned: AlignedMemoryMap,
// TODO: unaligned memory map?

pub fn init(regions: []const Region, version: sbpf.SBPFVersion) !MemoryMap {
pub fn init(regions: []const Region, version: sbpf.Version) !MemoryMap {
return .{ .aligned = try AlignedMemoryMap.init(regions, version) };
}

Expand Down Expand Up @@ -113,9 +113,9 @@ pub const Region = struct {

const AlignedMemoryMap = struct {
regions: []const Region,
version: sbpf.SBPFVersion,
version: sbpf.Version,

fn init(regions: []const Region, version: sbpf.SBPFVersion) !AlignedMemoryMap {
fn init(regions: []const Region, version: sbpf.Version) !AlignedMemoryMap {
for (regions, 1..) |reg, index| {
if (reg.vm_addr_start >> VIRTUAL_ADDRESS_BITS != index) {
return error.InvalidMemoryRegion;
Expand Down Expand Up @@ -162,7 +162,7 @@ test "aligned vmap" {
const m = try MemoryMap.init(&.{
Region.init(.mutable, &program_mem, PROGRAM_START),
Region.init(.constant, &stack_mem, STACK_START),
}, .v1);
}, .v0);

try expectEqual(
program_mem[0..1],
Expand Down Expand Up @@ -198,7 +198,7 @@ test "aligned region" {
const m = try MemoryMap.init(&.{
Region.init(.mutable, &program_mem, PROGRAM_START),
Region.init(.constant, &stack_mem, STACK_START),
}, .v1);
}, .v0);

try expectError(
error.AccessNotMapped,
Expand Down Expand Up @@ -244,6 +244,6 @@ test "invalid memory region" {
MemoryMap.init(&.{
Region.init(.constant, &stack_mem, STACK_START),
Region.init(.mutable, &program_mem, PROGRAM_START),
}, .v1),
}, .v0),
);
}
47 changes: 24 additions & 23 deletions src/svm/sbpf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const std = @import("std");
const memory = @import("memory.zig");
const assert = std.debug.assert;

pub const EF_SBPF_V2: u32 = 0x20;
pub const EF_SBPF_v1: u32 = 0x20;

/// Solana BPF Elf Machine
pub const EM_SBPF: std.elf.Elf64_Half = 263;
Expand All @@ -14,47 +14,48 @@ pub const EI_OSABI: u8 = 7;

pub const MAX_FILE_SIZE = 10 * 1024 * 1024;

pub const SBPFVersion = enum {
// v0,
pub const Version = enum(u32) {
v0,
v1,
v2,
// v1,
// v3,
// reserved,
/// support other versions as well!
_,

pub fn enableDynamicStackFrames(version: SBPFVersion) bool {
return version != .v1;
pub fn enableDynamicStackFrames(version: Version) bool {
return version != .v0;
}

pub fn enableElfVaddr(version: SBPFVersion) bool {
return version != .v1;
pub fn enableElfVaddr(version: Version) bool {
return version != .v0;
}

pub fn enableLDDW(version: SBPFVersion) bool {
return version == .v1;
pub fn enableLDDW(version: Version) bool {
return version == .v0;
}

pub fn enableStaticSyscalls(version: SBPFVersion) bool {
return version != .v1;
pub fn enableStaticSyscalls(version: Version) bool {
return version != .v0;
}

pub fn enableNegation(version: SBPFVersion) bool {
return version == .v1;
pub fn enableNegation(version: Version) bool {
return version == .v0;
}

pub fn enableLe(version: SBPFVersion) bool {
return version == .v1;
pub fn enableLe(version: Version) bool {
return version == .v0;
}

pub fn rejectRodataStackOverlap(version: SBPFVersion) bool {
return version != .v1;
pub fn rejectRodataStackOverlap(version: Version) bool {
return version != .v0;
}

pub fn enablePqr(version: SBPFVersion) bool {
return version != .v1;
pub fn enablePqr(version: Version) bool {
return version != .v0;
}

pub fn swapSubRegImmOperands(version: SBPFVersion) bool {
return version != .v1;
pub fn swapSubRegImmOperands(version: Version) bool {
return version != .v0;
}
};

Expand Down
Loading

0 comments on commit 7781166

Please sign in to comment.