From 54e0cede4384b7c1655f9183577bfccc11d9a7d5 Mon Sep 17 00:00:00 2001 From: Andrey Konovalov Date: Tue, 10 Jan 2017 16:45:52 +0100 Subject: prog: add bitfields to templates Now it's possible to use `int32:18` to denote a bitfield of size 18 as a struct field. This fixes #72. --- executor/common.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'executor/common.h') diff --git a/executor/common.h b/executor/common.h index 004dcc341..40b2bf8d0 100644 --- a/executor/common.h +++ b/executor/common.h @@ -158,6 +158,18 @@ static void install_segv_handler() __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \ } +#define BITMASK_LEN(type, bf_len) (type)((1ul << bf_len) - 1) + +#define BITMASK_LEN_OFF(type, bf_off, bf_len) (type)(BITMASK_LEN(type, bf_len) << bf_off) + +#define STORE_BY_BITMASK(type, addr, val, bf_off, bf_len) \ + do { \ + type new_val = *(type*)addr; \ + new_val &= ~BITMASK_LEN_OFF(type, bf_off, bf_len); \ + new_val |= ((type)val & BITMASK_LEN(type, bf_len)) << bf_off; \ + *(type*)addr = new_val; \ + } while (0) + #ifdef __NR_syz_emit_ethernet static void vsnprintf_check(char* str, size_t size, const char* format, va_list args) { -- cgit mrf-deployment