aboutgitcodelistschat:MatrixIRC
path: root/cooker/emit.c
diff options
context:
space:
mode:
authorStefano Brivio <sbrivio@redhat.com>2023-05-02 09:48:50 +0200
committerStefano Brivio <sbrivio@redhat.com>2023-05-02 10:39:32 +0200
commit82b77505f9420f11d614c2ae0f74153ca4ee3cb5 (patch)
treeec93abd39cd8c7ff2818c7446da6cc01864b2ae1 /cooker/emit.c
parenta2c20b2caf4e9cedb32b0930ffd7ca2e9ec72086 (diff)
downloadseitan-82b77505f9420f11d614c2ae0f74153ca4ee3cb5.tar
seitan-82b77505f9420f11d614c2ae0f74153ca4ee3cb5.tar.gz
seitan-82b77505f9420f11d614c2ae0f74153ca4ee3cb5.tar.bz2
seitan-82b77505f9420f11d614c2ae0f74153ca4ee3cb5.tar.lz
seitan-82b77505f9420f11d614c2ae0f74153ca4ee3cb5.tar.xz
seitan-82b77505f9420f11d614c2ae0f74153ca4ee3cb5.tar.zst
seitan-82b77505f9420f11d614c2ae0f74153ca4ee3cb5.zip
cooker updates spilling all over the place
Only tangentially related: - make seitan C99 again, so that I can build cooker without warnings - make Makefiles make use of the usual conventions about assigning directory paths in variables, drop numbers.h as requirement for cooker and make it convenient to run stand-alone Makefiles - fix up nr_syscalls.sh to be POSIX, otherwise it will give syntax errors on my system - define a single, common way to refer to offsets in gluten, and functions to use those offsets in a safe way. Immediates are gone: cooker will write any bit of "data" to the read-only section - call const what has to be const - define on-disk layout for gluten - add OP_NR (to check syscall numbers), rename OP_COPY_ARGS to OP_LOAD (it loads _selected_ stuff from arguments) As for cooker itself: - drop ARG_ and arg_ prefixes from struct names, and similar - add/rework functions to build OP_NR, OP_LOAD, OP_CMP, and to write constant data to gluten - add parsing for "compound" arguments, but that's not completely hooked into evaluation for numeric arguments yet Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
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;
}