Skip to content

Commit

Permalink
Remove unused string table code (#165)
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
  • Loading branch information
Alan-Jowett committed Oct 18, 2022
1 parent fd96f03 commit 9804aef
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 160 deletions.
4 changes: 0 additions & 4 deletions vm/ubpf_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,4 @@ struct ebpf_inst ubpf_fetch_instruction(const struct ubpf_vm *vm, uint16_t pc);
*/
void ubpf_store_instruction(const struct ubpf_vm *vm, uint16_t pc, struct ebpf_inst inst);


extern const char* ubpf_string_table[1];
#define UBPF_STRING_ID_DIVIDE_BY_ZERO 0

#endif
72 changes: 0 additions & 72 deletions vm/ubpf_jit_arm64.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ struct jump {
uint32_t target_pc;
};

struct string_reference {
uint32_t offset_loc;
uint32_t string_id;
};

struct jit_state {
uint8_t *buf;
uint32_t offset;
Expand All @@ -60,9 +55,6 @@ struct jit_state {
uint32_t unwind_loc;
struct jump *jumps;
int num_jumps;
struct string_reference* strings;
int num_strings;
uint32_t string_table_loc;
uint32_t stack_size;
};

Expand Down Expand Up @@ -392,18 +384,6 @@ emit_movewide_immediate(struct jit_state *state, bool sixty_four, enum Registers
}
}

static void update_adr_immediate(struct jit_state *state, uint32_t offset, int64_t imm21)
{
assert((imm21 >> 21) == 0 || (imm21 >> 21) == INT64_C(-1));

/* Read-modify-write the instruction. */
uint32_t instr;
memcpy(&instr, state->buf + offset, sizeof(uint32_t));
instr |= (imm21 & 3) << 29;
instr |= ((imm21 >> 2) & 0x3ffff) << 5;
memcpy(state->buf + offset, &instr, sizeof(uint32_t));
}

static void update_branch_immediate(struct jit_state *state, uint32_t offset, int32_t imm)
{
assert((imm & 3) == 0);
Expand Down Expand Up @@ -458,22 +438,6 @@ emit_function_prologue(struct jit_state *state, size_t ubpf_stack_size)
emit_addsub_immediate(state, true, AS_ADD, map_register(10), SP, state->stack_size);
}

#if 0
static void
emit_string_load(struct jit_state *state, enum Registers dst, int string_id)
{
if (state->num_strings == UBPF_MAX_INSTS) {
return;
}

state->strings[state->num_strings].offset_loc = state->offset;
state->strings[state->num_strings].string_id = string_id;
// ADR dst, #0 - will be fixed up later.
emit_instruction(state, (0 << 29) | (1 << 28) | (0 << 5) | dst);
state->num_strings++;
}
#endif

static void emit_call(struct jit_state *state, uintptr_t func) {
emit_movewide_immediate(state, true, temp_register, func);
emit_unconditonalbranch_register(state, BR_BLR, temp_register);
Expand Down Expand Up @@ -985,11 +949,6 @@ translate(struct ubpf_vm *vm, struct jit_state *state, char **errmsg)
}

emit_function_epilogue(state);
// Emit string table.
state->string_table_loc = state->offset;
for (i = 0; i < _countof(ubpf_string_table); i++) {
emit_bytes(state, (void*)ubpf_string_table[i], strlen(ubpf_string_table[i]) + 1);
}

return 0;
}
Expand Down Expand Up @@ -1028,28 +987,6 @@ resolve_jumps(struct jit_state *state)
}
}

static uint32_t
string_offset_from_id(struct jit_state *state, uint32_t string_id)
{
uint32_t offset = state->string_table_loc;
uint32_t i;
for (i = 0; i < string_id; i ++) {
offset += strlen(ubpf_string_table[i]) + 1;
}
return offset;
}

static void
resolve_strings(struct jit_state *state)
{
int i;
for (i = 0; i < state->num_strings; i++) {
struct string_reference string = state->strings[i];
int64_t rel = string_offset_from_id(state, string.string_id) - string.offset_loc;
update_adr_immediate(state, string.offset_loc, rel);
}
}

int
ubpf_translate_arm64(struct ubpf_vm *vm, uint8_t *buffer, size_t *size, char **errmsg)
{
Expand All @@ -1061,9 +998,7 @@ ubpf_translate_arm64(struct ubpf_vm *vm, uint8_t *buffer, size_t *size, char **e
state.buf = buffer;
state.pc_locs = calloc(UBPF_MAX_INSTS+1, sizeof(state.pc_locs[0]));
state.jumps = calloc(UBPF_MAX_INSTS, sizeof(state.jumps[0]));
state.strings = calloc(UBPF_MAX_INSTS, sizeof(state.strings[0]));
state.num_jumps = 0;
state.num_strings = 0;

if (translate(vm, &state, errmsg) < 0) {
goto out;
Expand All @@ -1074,25 +1009,18 @@ ubpf_translate_arm64(struct ubpf_vm *vm, uint8_t *buffer, size_t *size, char **e
goto out;
}

if (state.num_strings == UBPF_MAX_INSTS) {
*errmsg = ubpf_error("Excessive number of string targets");
goto out;
}

if (state.offset == state.size) {
*errmsg = ubpf_error("Target buffer too small");
goto out;
}

resolve_jumps(&state);
resolve_strings(&state);
result = 0;

*size = state.offset;

out:
free(state.pc_locs);
free(state.jumps);
free(state.strings);
return result;
}
55 changes: 0 additions & 55 deletions vm/ubpf_jit_x86_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,27 +490,6 @@ translate(struct ubpf_vm *vm, struct jit_state *state, char **errmsg)

emit1(state, 0xc3); /* ret */

/* Division by zero handler */

// Save the address of the start of the divide by zero handler.
state->div_by_zero_loc = state->offset;

// RCX is the first parameter register for Windows, so first save the value.
emit_mov(state, RCX, platform_parameter_registers[2]); /* muldivmod stored pc in RCX */
emit_string_load(state, platform_parameter_registers[1], UBPF_STRING_ID_DIVIDE_BY_ZERO);
emit_load_imm(state, platform_parameter_registers[0], (uintptr_t)stderr);
emit_call(state, vm->error_printf);

emit_load_imm(state, map_register(0), -1);
emit_jmp(state, TARGET_PC_EXIT);


// Emit string table.
state->string_table_loc = state->offset;
for (i = 0; i < _countof(ubpf_string_table); i++) {
emit_bytes(state, (void*)ubpf_string_table[i], strlen(ubpf_string_table[i]) + 1);
}

return 0;
}

Expand Down Expand Up @@ -662,31 +641,6 @@ resolve_jumps(struct jit_state *state)
}
}

static uint32_t
string_offset_from_id(struct jit_state *state, uint32_t string_id)
{
uint32_t offset = state->string_table_loc;
uint32_t i;
for (i = 0; i < string_id; i ++) {
offset += strlen(ubpf_string_table[i]) + 1;
}
return offset;
}

static void
resolve_strings(struct jit_state *state)
{
int i;
for (i = 0; i < state->num_strings; i++) {
struct string_reference string = state->strings[i];
uint32_t rel = string_offset_from_id(state, string.string_id) - (string.offset_loc);

uint8_t *offset_ptr = &state->buf[string.offset_loc - sizeof(uint32_t)];
memcpy(offset_ptr, &rel, sizeof(uint32_t));
}
}


int
ubpf_translate_x86_64(struct ubpf_vm *vm, uint8_t * buffer, size_t * size, char **errmsg)
{
Expand All @@ -698,9 +652,7 @@ ubpf_translate_x86_64(struct ubpf_vm *vm, uint8_t * buffer, size_t * size, char
state.buf = buffer;
state.pc_locs = calloc(UBPF_MAX_INSTS+1, sizeof(state.pc_locs[0]));
state.jumps = calloc(UBPF_MAX_INSTS, sizeof(state.jumps[0]));
state.strings = calloc(UBPF_MAX_INSTS, sizeof(state.strings[0]));
state.num_jumps = 0;
state.num_strings = 0;

if (translate(vm, &state, errmsg) < 0) {
goto out;
Expand All @@ -711,25 +663,18 @@ ubpf_translate_x86_64(struct ubpf_vm *vm, uint8_t * buffer, size_t * size, char
goto out;
}

if (state.num_strings == UBPF_MAX_INSTS) {
*errmsg = ubpf_error("Excessive number of string targets");
goto out;
}

if (state.offset == state.size) {
*errmsg = ubpf_error("Target buffer too small");
goto out;
}

resolve_jumps(&state);
resolve_strings(&state);
result = 0;

*size = state.offset;

out:
free(state.pc_locs);
free(state.jumps);
free(state.strings);
return result;
}
25 changes: 0 additions & 25 deletions vm/ubpf_jit_x86_64.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ struct jump {
uint32_t target_pc;
};

struct string_reference {
uint32_t offset_loc;
uint32_t string_id;
};

struct jit_state {
uint8_t *buf;
uint32_t offset;
Expand All @@ -70,9 +65,6 @@ struct jit_state {
uint32_t unwind_loc;
struct jump *jumps;
int num_jumps;
struct string_reference* strings;
int num_strings;
uint32_t string_table_loc;
};

static inline void
Expand Down Expand Up @@ -364,21 +356,4 @@ emit_jmp(struct jit_state *state, uint32_t target_pc)
emit_jump_offset(state, target_pc);
}

/* emit "lea dst,[rip+0x0]" and store offset + string id */
static inline void
emit_string_load(struct jit_state *state, int dst, int string_id)
{
if (state->num_strings == UBPF_MAX_INSTS) {
return;
}

emit_basic_rex(state, 1, RIP, dst);
emit1(state, 0x8d);
emit_modrm(state, 0, dst, RIP);
emit4(state, 0);
state->strings[state->num_strings].offset_loc = state->offset;
state->strings[state->num_strings].string_id = string_id;
state->num_strings++;
}

#endif
4 changes: 0 additions & 4 deletions vm/ubpf_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@

#define MAX_EXT_FUNCS 64

const char* ubpf_string_table[1] = {
"uBPF error: division by zero at PC %u\n",
};

static bool validate(const struct ubpf_vm *vm, const struct ebpf_inst *insts, uint32_t num_insts, char **errmsg);
static bool bounds_check(const struct ubpf_vm *vm, void *addr, int size, const char *type, uint16_t cur_pc, void *mem, size_t mem_len, void *stack);

Expand Down

0 comments on commit 9804aef

Please sign in to comment.