From 30cea208811fa484e58534043b36e9172c887328 Mon Sep 17 00:00:00 2001 From: Emanuele Cesena Date: Fri, 6 Dec 2024 23:22:48 +0000 Subject: [PATCH] vm: simplify use of offset --- src/flamenco/vm/fd_vm_interp_core.c | 72 ++++++++++++------------ src/flamenco/vm/fd_vm_private.h | 2 +- src/flamenco/vm/jit/fd_jit_compiler.c | 2 +- src/flamenco/vm/jit/fd_jit_compiler.dasc | 2 +- 4 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/flamenco/vm/fd_vm_interp_core.c b/src/flamenco/vm/fd_vm_interp_core.c index 8476fe8eb7..fdf4bb41f5 100644 --- a/src/flamenco/vm/fd_vm_interp_core.c +++ b/src/flamenco/vm/fd_vm_interp_core.c @@ -135,7 +135,7 @@ ulong opcode; ulong dst; ulong src; - short offset; + ulong offset; /* offset is 16-bit but always sign extended, so we handle cast once */ uint imm; ulong reg_dst; ulong reg_src; @@ -312,7 +312,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x05) /* FD_SBPF_OP_JA */ - pc += (ulong)(long)offset; + pc += offset; FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x07) /* FD_SBPF_OP_ADD64_IMM */ @@ -342,7 +342,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x15) /* FD_SBPF_OP_JEQ_IMM */ - pc += fd_ulong_if( reg_dst==(ulong)(long)(int)imm, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( reg_dst==(ulong)(long)(int)imm, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x17) /* FD_SBPF_OP_SUB64_IMM */ @@ -370,7 +370,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x1d) /* FD_SBPF_OP_JEQ_REG */ - pc += fd_ulong_if( reg_dst==reg_src, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( reg_dst==reg_src, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x1f) /* FD_SBPF_OP_SUB64_REG */ @@ -384,12 +384,12 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x25) /* FD_SBPF_OP_JGT_IMM */ - pc += fd_ulong_if( reg_dst>(ulong)(long)(int)imm, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( reg_dst>(ulong)(long)(int)imm, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x27) { /* FD_SBPF_OP_STB */ uchar is_multi_region = 0; - ulong vaddr = reg_dst + (ulong)(long)offset; + ulong vaddr = reg_dst + offset; ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(uchar), region_haddr, region_st_sz, 1, 0UL, &is_multi_region ); if( FD_UNLIKELY( !haddr ) ) { vm->segv_store_vaddr = vaddr; goto sigsegv; } /* Note: untaken branches don't consume BTB */ fd_vm_mem_st_1( haddr, (uchar)imm ); @@ -398,7 +398,7 @@ FD_VM_INTERP_INSTR_BEGIN(0x2c) { /* FD_SBPF_OP_LDXB */ uchar is_multi_region = 0; - ulong vaddr = reg_src + (ulong)(long)offset; + ulong vaddr = reg_src + offset; ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(uchar), region_haddr, region_ld_sz, 0, 0UL, &is_multi_region ); if( FD_UNLIKELY( !haddr ) ) goto sigsegv; /* Note: untaken branches don't consume BTB */ reg[ dst ] = fd_vm_mem_ld_1( haddr ); @@ -406,12 +406,12 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x2d) /* FD_SBPF_OP_JGT_REG */ - pc += fd_ulong_if( reg_dst>reg_src, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( reg_dst>reg_src, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x2f) { /* FD_SBPF_OP_STXB */ uchar is_multi_region = 0; - ulong vaddr = reg_dst + (ulong)(long)offset; + ulong vaddr = reg_dst + offset; ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(uchar), region_haddr, region_st_sz, 1, 0UL, &is_multi_region ); if( FD_UNLIKELY( !haddr ) ) { vm->segv_store_vaddr = vaddr; goto sigsegv; } /* Note: untaken branches don't consume BTB */ /* FIXME: sigrdonly */ fd_vm_mem_st_1( haddr, (uchar)reg_src ); @@ -439,7 +439,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x35) /* FD_SBPF_OP_JGE_IMM */ - pc += fd_ulong_if( reg_dst>=(ulong)(long)(int)imm, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( reg_dst>=(ulong)(long)(int)imm, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x36) /* FD_SBPF_OP_UHMUL64_IMM */ @@ -448,7 +448,7 @@ FD_VM_INTERP_INSTR_BEGIN(0x37) { /* FD_SBPF_OP_STH */ uchar is_multi_region = 0; - ulong vaddr = reg_dst + (ulong)(long)offset; + ulong vaddr = reg_dst + offset; ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(ushort), region_haddr, region_st_sz, 1, 0UL, &is_multi_region ); int sigsegv = !haddr; if( FD_UNLIKELY( sigsegv ) ) { vm->segv_store_vaddr = vaddr; goto sigsegv; } /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */ @@ -458,7 +458,7 @@ FD_VM_INTERP_INSTR_BEGIN(0x3c) { /* FD_SBPF_OP_LDXH */ uchar is_multi_region = 0; - ulong vaddr = reg_src + (ulong)(long)offset; + ulong vaddr = reg_src + offset; ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(ushort), region_haddr, region_ld_sz, 0, 0UL, &is_multi_region ); int sigsegv = !haddr; if( FD_UNLIKELY( sigsegv ) ) goto sigsegv; /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */ @@ -467,12 +467,12 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x3d) /* FD_SBPF_OP_JGE_REG */ - pc += fd_ulong_if( reg_dst>=reg_src, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( reg_dst>=reg_src, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x3f) { /* FD_SBPF_OP_STXH */ uchar is_multi_region = 0; - ulong vaddr = reg_dst + (ulong)(long)offset; + ulong vaddr = reg_dst + offset; ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(ushort), region_haddr, region_st_sz, 1, 0UL, &is_multi_region ); int sigsegv = !haddr; if( FD_UNLIKELY( sigsegv ) ) { vm->segv_store_vaddr = vaddr; goto sigsegv; } /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus/rdonly */ @@ -505,7 +505,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x45) /* FD_SBPF_OP_JSET_IMM */ - pc += fd_ulong_if( !!(reg_dst & (ulong)(long)(int)imm), (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( !!(reg_dst & (ulong)(long)(int)imm), offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x46) /* FD_SBPF_OP_UDIV32_IMM */ @@ -521,7 +521,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x4d) /* FD_SBPF_OP_JSET_REG */ - pc += fd_ulong_if( !!(reg_dst & reg_src), (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( !!(reg_dst & reg_src), offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x4e) /* FD_SBPF_OP_UDIV32_REG */ @@ -540,7 +540,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x55) /* FD_SBPF_OP_JNE_IMM */ - pc += fd_ulong_if( reg_dst!=(ulong)(long)(int)imm, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( reg_dst!=(ulong)(long)(int)imm, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x56) /* FD_SBPF_OP_UDIV64_IMM */ @@ -556,7 +556,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x5d) /* FD_SBPF_OP_JNE_REG */ - pc += fd_ulong_if( reg_dst!=reg_src, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( reg_dst!=reg_src, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x5e) /* FD_SBPF_OP_UDIV64_REG */ @@ -580,7 +580,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x65) /* FD_SBPF_OP_JSGT_IMM */ - pc += fd_ulong_if( (long)reg_dst>(long)(int)imm, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( (long)reg_dst>(long)(int)imm, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x66) /* FD_SBPF_OP_UREM32_IMM */ @@ -598,7 +598,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x6d) /* FD_SBPF_OP_JSGT_REG */ - pc += fd_ulong_if( (long)reg_dst>(long)reg_src, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( (long)reg_dst>(long)reg_src, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x6e) /* FD_SBPF_OP_UREM32_REG */ @@ -619,7 +619,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x75) /* FD_SBPF_OP_JSGE_IMM */ - pc += fd_ulong_if( (long)reg_dst>=(long)(int)imm, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( (long)reg_dst>=(long)(int)imm, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x76) /* FD_SBPF_OP_UREM64_IMM */ @@ -637,7 +637,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0x7d) /* FD_SBPF_OP_JSGE_REG */ - pc += fd_ulong_if( (long)reg_dst>=(long)reg_src, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( (long)reg_dst>=(long)reg_src, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0x7e) /* FD_SBPF_OP_UREM64_REG */ @@ -782,7 +782,7 @@ FD_VM_INTERP_INSTR_BEGIN(0x87) { /* FD_SBPF_OP_STW */ uchar is_multi_region = 0; - ulong vaddr = reg_dst + (ulong)(long)offset; + ulong vaddr = reg_dst + offset; ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(uint), region_haddr, region_st_sz, 1, 0UL, &is_multi_region ); int sigsegv = !haddr; if( FD_UNLIKELY( sigsegv ) ) { vm->segv_store_vaddr = vaddr; goto sigsegv; } /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */ @@ -795,7 +795,7 @@ FD_VM_INTERP_INSTR_BEGIN(0x8c) { /* FD_SBPF_OP_LDXW */ uchar is_multi_region = 0; - ulong vaddr = reg_src + (ulong)(long)offset; + ulong vaddr = reg_src + offset; ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(uint), region_haddr, region_ld_sz, 0, 0UL, &is_multi_region ); int sigsegv = !haddr; if( FD_UNLIKELY( sigsegv ) ) goto sigsegv; /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */ @@ -847,7 +847,7 @@ FD_VM_INTERP_INSTR_BEGIN(0x8f) { /* FD_SBPF_OP_STXW */ uchar is_multi_region = 0; - ulong vaddr = reg_dst + (ulong)(long)offset; + ulong vaddr = reg_dst + offset; ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(uint), region_haddr, region_st_sz, 1, 0UL, &is_multi_region ); int sigsegv = !haddr; if( FD_UNLIKELY( sigsegv ) ) { vm->segv_store_vaddr = vaddr; goto sigsegv; } /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus/rdonly */ @@ -883,7 +883,7 @@ FD_VM_INTERP_INSTR_BEGIN(0x97) { /* FD_SBPF_OP_STQ */ uchar is_multi_region = 0; - ulong vaddr = reg_dst + (ulong)(long)offset; + ulong vaddr = reg_dst + offset; ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(ulong), region_haddr, region_st_sz, 1, 0UL, &is_multi_region ); int sigsegv = !haddr; if( FD_UNLIKELY( sigsegv ) ) { vm->segv_store_vaddr = vaddr; goto sigsegv; } /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */ @@ -893,7 +893,7 @@ FD_VM_INTERP_INSTR_BEGIN(0x9c) { /* FD_SBPF_OP_LDXQ */ uchar is_multi_region = 0; - ulong vaddr = reg_src + (ulong)(long)offset; + ulong vaddr = reg_src + offset; ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(ulong), region_haddr, region_ld_sz, 0, 0UL, &is_multi_region ); int sigsegv = !haddr; if( FD_UNLIKELY( sigsegv ) ) goto sigsegv; /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */ @@ -907,7 +907,7 @@ FD_VM_INTERP_INSTR_BEGIN(0x9f) { /* FD_SBPF_OP_STXQ */ uchar is_multi_region = 0; - ulong vaddr = reg_dst + (ulong)(long)offset; + ulong vaddr = reg_dst + offset; ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(ulong), region_haddr, region_st_sz, 1, 0UL, &is_multi_region ); int sigsegv = !haddr; if( FD_UNLIKELY( sigsegv ) ) { vm->segv_store_vaddr = vaddr; goto sigsegv; } /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus/rdonly */ @@ -936,7 +936,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0xa5) /* FD_SBPF_OP_JLT_IMM */ - pc += fd_ulong_if( reg_dst<(ulong)(long)(int)imm, (ulong)(long)offset, 0UL ); + pc += fd_ulong_if( reg_dst<(ulong)(long)(int)imm, offset, 0UL ); FD_VM_INTERP_BRANCH_END; FD_VM_INTERP_INSTR_BEGIN(0xa7) /* FD_SBPF_OP_XOR64_IMM */ @@ -948,7 +948,7 @@ FD_VM_INTERP_INSTR_END; FD_VM_INTERP_BRANCH_BEGIN(0xad) /* FD_SBPF_OP_JLT_REG */ - pc += fd_ulong_if( reg_dst> 8) & 15UL); } /* In [0,16) */ FD_FN_CONST static inline ulong fd_vm_instr_src ( ulong instr ) { return ((instr>>12) & 15UL); } /* In [0,16) */ -FD_FN_CONST static inline short fd_vm_instr_offset( ulong instr ) { return (short)(ushort)(instr>>16); } +FD_FN_CONST static inline ulong fd_vm_instr_offset( ulong instr ) { return (ulong)(long)(short)(ushort)(instr>>16); } FD_FN_CONST static inline uint fd_vm_instr_imm ( ulong instr ) { return (uint)(instr>>32); } FD_FN_CONST static inline ulong fd_vm_instr_opclass ( ulong instr ) { return instr & 7UL; } /* In [0,8) */ diff --git a/src/flamenco/vm/jit/fd_jit_compiler.c b/src/flamenco/vm/jit/fd_jit_compiler.c index c1beb3d244..32c2da97be 100644 --- a/src/flamenco/vm/jit/fd_jit_compiler.c +++ b/src/flamenco/vm/jit/fd_jit_compiler.c @@ -724,7 +724,7 @@ fd_jit_compile( struct dasm_State ** Dst, ulong opcode = fd_vm_instr_opcode( instr ); /* in [0,256) even if malformed */ ulong dst = fd_vm_instr_dst ( instr ); /* in [0, 16) even if malformed */ ulong src = fd_vm_instr_src ( instr ); /* in [0, 16) even if malformed */ - short offset = fd_vm_instr_offset( instr ); /* in [-2^15,2^15) even if malformed */ + ulong offset = fd_vm_instr_offset( instr ); /* in [-2^15,2^15) even if malformed */ uint imm = fd_vm_instr_imm ( instr ); /* in [0,2^32) even if malformed */ /* Macros for translating register accesses */ diff --git a/src/flamenco/vm/jit/fd_jit_compiler.dasc b/src/flamenco/vm/jit/fd_jit_compiler.dasc index ec0e15a339..49a783f3be 100644 --- a/src/flamenco/vm/jit/fd_jit_compiler.dasc +++ b/src/flamenco/vm/jit/fd_jit_compiler.dasc @@ -565,7 +565,7 @@ fd_jit_compile( struct dasm_State ** Dst, ulong opcode = fd_vm_instr_opcode( instr ); /* in [0,256) even if malformed */ ulong dst = fd_vm_instr_dst ( instr ); /* in [0, 16) even if malformed */ ulong src = fd_vm_instr_src ( instr ); /* in [0, 16) even if malformed */ - short offset = fd_vm_instr_offset( instr ); /* in [-2^15,2^15) even if malformed */ + ulong offset = fd_vm_instr_offset( instr ); /* in [-2^15,2^15) even if malformed */ uint imm = fd_vm_instr_imm ( instr ); /* in [0,2^32) even if malformed */ /* Macros for translating register accesses */