aboutgitcodelistschat:MatrixIRC
path: root/cooker/emit.c
diff options
context:
space:
mode:
Diffstat (limited to 'cooker/emit.c')
-rw-r--r--cooker/emit.c94
1 files changed, 87 insertions, 7 deletions
diff --git a/cooker/emit.c b/cooker/emit.c
index a82529c..8c35f1d 100644
--- a/cooker/emit.c
+++ b/cooker/emit.c
@@ -2,7 +2,7 @@
/* seitan - Syscall Expressive Interpreter, Transformer and Notifier
*
- * cooker/emit.c - Generate gluten (bytecode) instructions
+ * cooker/emit.c - Generate gluten (bytecode) instructions and read-only data
*
* Copyright 2023 Red Hat GmbH
* Author: Stefano Brivio <sbrivio@redhat.com>
@@ -11,17 +11,97 @@
#include "cooker.h"
#include "gluten.h"
#include "util.h"
+#include "emit.h"
-int emit_nr(struct gluten_ctx *g, long number)
+static const char *type_str[] = {
+ "INT", "INTMASK", "INTFLAGS",
+ "U32", "U32MASK", "U32FLAGS",
+ "LONG", "LONGMASK", "LONGFLAGS",
+ "STRING",
+ "STRUCT", "SELECT",
+ "PID",
+ "PORT", "IPV4", "IPV6",
+ "FDPATH",
+ NULL
+};
+
+static const char *cmp_type_str[] = { "EQ", "GT", "GE", "LT", "LE", NULL };
+
+void emit_nr(struct gluten_ctx *g, long number)
+{
+ struct op_nr *nr = (struct op_nr *)gluten_ptr(&g->g, g->ip);
+
+ nr->nr = number;
+ nr->no_match.type = OFFSET_INSTRUCTION;
+ nr->no_match.offset = NEXT_BLOCK;
+
+ debug(" %i: OP_NR %li, < >", g->ip.offset, number);
+
+ if (++g->ip.offset > INST_MAX)
+ die("Too many instructions");
+}
+
+void emit_load(struct gluten_ctx *g, struct gluten_offset dst,
+ int index, size_t len)
+{
+ struct op_load *load = (struct op_load *)gluten_ptr(&g->g, g->ip);
+
+ load->src.type = OFFSET_SECCOMP_DATA;
+ load->src.offset = index;
+
+ load->dst = dst;
+
+ debug(" %i: OP_LOAD #%i < %i (%lu)", g->ip.offset, dst.offset,
+ index, len);
+
+ if (++g->ip.offset > INST_MAX)
+ die("Too many instructions");
+}
+
+void emit_cmp(struct gluten_ctx *g, enum op_cmp_type cmp,
+ struct gluten_offset x, struct gluten_offset y, size_t size,
+ enum jump_type jmp)
{
- debug(" %i: OP_NR %li, < >", g->ip++, number);
+ struct op_cmp *op = (struct op_cmp *)gluten_ptr(&g->g, g->ip);
+
+ op->x = x;
+ op->y = y;
+ op->size = size;
+ op->cmp = cmp;
+ op->jmp = jmp;
+
+ debug(" %i: OP_CMP (#%lu) %%%lu %s %%%lu", g->ip.offset, size,
+ x.offset, cmp_type_str[cmp], y.offset);
- return 0;
+ if (++g->ip.offset > INST_MAX)
+ die("Too many instructions");
}
-int emit_load(struct gluten_ctx *g, int offset, int index, size_t len)
+void emit_cmp_field(struct gluten_ctx *g, enum op_cmp_type cmp,
+ struct field *field,
+ struct gluten_offset base, struct gluten_offset match,
+ enum jump_type jmp)
{
- debug(" %i: OP_LOAD #%i < %i (%lu)", g->ip++, offset, index, len);
+ base.offset += field->offset;
+
+ emit_cmp(g, cmp, base, match,
+ field->strlen ? field->strlen : gluten_size[field->type],
+ jmp);
+}
+
+struct gluten_offset emit_data(struct gluten_ctx *g, enum type type,
+ union value *value)
+{
+ void *p = gluten_ptr(&g->g, g->cp);
+ struct gluten_offset ret = g->cp;
+
+ if (type == INT) {
+ *(int *)p = value->v_int;
+ debug(" C#%i: (%s) %i", g->cp.offset, type_str[type],
+ value->v_int);
+ if ((g->cp.offset += sizeof(int)) > RO_DATA_SIZE)
+ die(" Read-only data section exceeded");
+ }
- return 0;
+ return ret;
}