From 1bdb387c18a852e75aef0b3c4ddb0481f90d66bd Mon Sep 17 00:00:00 2001 From: Andrey Konovalov Date: Thu, 18 May 2017 16:38:10 +0200 Subject: csource: emit bitmasks only when required --- csource/common.go | 35 ++++++++++++++++++++--------------- csource/csource.go | 21 ++++++--------------- 2 files changed, 26 insertions(+), 30 deletions(-) (limited to 'csource') diff --git a/csource/common.go b/csource/common.go index c0abd4412..3ac10334d 100644 --- a/csource/common.go +++ b/csource/common.go @@ -116,6 +116,22 @@ void debug(const char* msg, ...) fflush(stdout); } +#if defined(SYZ_EXECUTOR) || defined(SYZ_USE_BITMASKS) +#define BITMASK_LEN(type, bf_len) (type)((1ull << (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) \ + if ((bf_off) == 0 && (bf_len) == 0) { \ + *(type*)(addr) = (type)(val); \ + } else { \ + 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; \ + } +#endif + #if defined(SYZ_EXECUTOR) || defined(SYZ_HANDLE_SEGV) __thread int skip_segv; __thread jmp_buf segv_env; @@ -175,20 +191,6 @@ static void use_temporary_dir() } #endif -#define BITMASK_LEN(type, bf_len) (type)((1ull << (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) \ - if ((bf_off) == 0 && (bf_len) == 0) { \ - *(type*)(addr) = (type)(val); \ - } else { \ - 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; \ - } - #if defined(SYZ_EXECUTOR) || defined(SYZ_TUN_ENABLE) static void vsnprintf_check(char* str, size_t size, const char* format, va_list args) { @@ -551,7 +553,10 @@ static uintptr_t syz_fuseblk_mount(uintptr_t a0, uintptr_t a1, uintptr_t a2, uin #ifndef NONFAILING -#define NONFAILING(x) { x; } +#define NONFAILING(x) \ + { \ + x; \ + } #endif const char kvm_asm16_cpl3[] = "\x0f\x20\xc0\x66\x83\xc8\x01\x0f\x22\xc0\xb8\xa0\x00\x0f\x00\xd8\xb8\x2b\x00\x8e\xd8\x8e\xc0\x8e\xe0\x8e\xe8\xbc\x00\x01\xc7\x06\x00\x01\x1d\xba\xc7\x06\x02\x01\x23\x00\xc7\x06\x04\x01\x00\x01\xc7\x06\x06\x01\x2b\x00\xcb"; diff --git a/csource/csource.go b/csource/csource.go index 0a5bcddd7..8faae8e5e 100644 --- a/csource/csource.go +++ b/csource/csource.go @@ -42,18 +42,6 @@ type Options struct { Repro bool } -func RequiresTun(p *prog.Prog) bool { - for _, c := range p.Calls { - switch c.Meta.CallName { - case "syz_emit_ethernet": - return true - case "syz_extract_tcp_seq": - return true - } - } - return false -} - func Write(p *prog.Prog, opts Options) ([]byte, error) { exec := make([]byte, prog.ExecBufferSize) if err := p.SerializeForExec(exec, 0); err != nil { @@ -61,7 +49,7 @@ func Write(p *prog.Prog, opts Options) ([]byte, error) { } w := new(bytes.Buffer) - if RequiresTun(p) { + if prog.RequiresTun(p) { opts.EnableTun = true } @@ -78,7 +66,7 @@ func Write(p *prog.Prog, opts Options) ([]byte, error) { } fmt.Fprintf(w, "\n") - hdr, err := preprocessCommonHeader(opts, handled) + hdr, err := preprocessCommonHeader(opts, handled, prog.RequiresBitmasks(p)) if err != nil { return nil, err } @@ -340,8 +328,11 @@ loop: return calls, n } -func preprocessCommonHeader(opts Options, handled map[string]int) (string, error) { +func preprocessCommonHeader(opts Options, handled map[string]int, useBitmasks bool) (string, error) { var defines []string + if useBitmasks { + defines = append(defines, "SYZ_USE_BITMASKS") + } switch opts.Sandbox { case "none": defines = append(defines, "SYZ_SANDBOX_NONE") -- cgit mrf-deployment