• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2017 syzkaller project authors. All rights reserved.
2// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3
4// XedDecode is required for tests, but it requires Intel XED library installed, so it is disabled by default.
5// To run full tests, check out and build github.com/intelxed/xed, then run:
6// INTELXED=/path/to/intelxed CGO_CFLAGS="-I $INTELXED/xed/include/public \
7//	-I $INTELXED/build/obj" CGO_LDFLAGS="$INTELXED/build/obj/libxed.a" \
8//	go test -v -tags xed
9
10// +build xed
11
12package ifuzz
13
14/*
15#include "xed-interface.h"
16
17int xedDecode(int mode, int addrsize, void* text, int size, const char** error) {
18  xed_decoded_inst_t xedd;
19  xed_decoded_inst_zero(&xedd);
20  xed_decoded_inst_set_mode(&xedd, mode, addrsize);
21  xed_error_enum_t err = xed_decode(&xedd, text, size);
22  if (err != XED_ERROR_NONE) {
23    if (error)
24      *error = xed_error_enum_t2str(err);
25    return 0;
26  }
27  return xed_decoded_inst_get_length(&xedd);
28}
29*/
30import "C"
31
32import (
33	"errors"
34	"unsafe"
35)
36
37func init() {
38	C.xed_tables_init()
39	XedDecode = xedDecode
40}
41
42func xedDecode(mode int, text []byte) (int, error) {
43	xedMode := 0
44	xedAddr := 0
45	switch mode {
46	case ModeLong64:
47		xedMode = C.XED_MACHINE_MODE_LONG_64
48		xedAddr = C.XED_ADDRESS_WIDTH_64b
49	case ModeProt32:
50		xedMode = C.XED_MACHINE_MODE_LONG_COMPAT_32
51		xedAddr = C.XED_ADDRESS_WIDTH_32b
52	case ModeProt16:
53		xedMode = C.XED_MACHINE_MODE_LONG_COMPAT_16
54		xedAddr = C.XED_ADDRESS_WIDTH_16b
55	case ModeReal16:
56		xedMode = C.XED_MACHINE_MODE_REAL_16
57		xedAddr = C.XED_ADDRESS_WIDTH_16b
58	default:
59		panic("bad mode")
60	}
61	var errorStr *C.char
62	res := C.xedDecode(C.int(xedMode), C.int(xedAddr), unsafe.Pointer(&text[0]), C.int(len(text)), &errorStr)
63	if res == 0 {
64		return 0, errors.New(C.GoString(errorStr))
65	}
66	return int(res), nil
67}
68