evoasm 0.0.2.pre7 → 0.1.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gdbinit +41 -0
- data/.gitignore +1 -2
- data/.gitmodules +3 -0
- data/.rubocop.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.md +660 -0
- data/Makefile +1 -1
- data/README.md +17 -9
- data/Rakefile +39 -107
- data/bin/gdb +1 -1
- data/bin/gdb_loop +4 -0
- data/docs/FindingInstructions.md +17 -0
- data/docs/JIT.md +14 -0
- data/docs/SymbolicRegression.md +102 -0
- data/docs/Visualization.md +29 -0
- data/docs/examples/bit_insts.rb +44 -0
- data/docs/examples/jit.rb +26 -0
- data/docs/examples/loss.gif +0 -0
- data/docs/examples/program.png +0 -0
- data/docs/examples/sym_reg.rb +64 -0
- data/docs/examples/vis.rb +38 -0
- data/evoasm.gemspec +21 -15
- data/ext/evoasm_ext/Rakefile +3 -0
- data/ext/evoasm_ext/compile.rake +35 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-alloc.c +226 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-alloc.h +84 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-arch.c +52 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-arch.h +101 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-bitmap.h +158 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-buf.c +204 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-buf.h +109 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-domain.c +124 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-domain.h +279 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-error.c +65 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-error.h +108 -0
- data/ext/evoasm_ext/{evoasm-log.c → libevoasm/src/evoasm-log.c} +36 -18
- data/ext/evoasm_ext/libevoasm/src/evoasm-log.h +93 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-param.c +22 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-param.h +33 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-pop-params.c +192 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-pop-params.h +60 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-pop.c +1323 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-pop.h +107 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-program-io.c +116 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-program-io.h +60 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-program.c +1827 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-program.h +167 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-rand.c +65 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-rand.h +76 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-signal.c +106 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-signal.h +58 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-util.h +112 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-x64.c +925 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-x64.h +277 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm.c +28 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm.h +35 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-enums.h +2077 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-insts.c +191203 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-insts.h +1713 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-misc.c +348 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-misc.h +93 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-params.c +51 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-params.h +509 -0
- data/lib/evoasm.rb +28 -11
- data/lib/evoasm/buffer.rb +105 -0
- data/lib/evoasm/capstone.rb +100 -0
- data/lib/evoasm/domain.rb +116 -0
- data/lib/evoasm/error.rb +37 -16
- data/lib/evoasm/exception_error.rb +19 -0
- data/lib/evoasm/ffi_ext.rb +53 -0
- data/lib/evoasm/libevoasm.rb +286 -0
- data/lib/evoasm/libevoasm/x64_enums.rb +1967 -0
- data/lib/evoasm/parameter.rb +20 -0
- data/lib/evoasm/population.rb +145 -0
- data/lib/evoasm/population/parameters.rb +227 -0
- data/lib/evoasm/population/plotter.rb +89 -0
- data/lib/evoasm/prng.rb +64 -0
- data/lib/evoasm/program.rb +195 -12
- data/lib/evoasm/program/io.rb +144 -0
- data/lib/evoasm/test.rb +8 -0
- data/lib/evoasm/version.rb +1 -1
- data/lib/evoasm/x64.rb +115 -0
- data/lib/evoasm/x64/cpu_state.rb +95 -0
- data/lib/evoasm/x64/instruction.rb +109 -0
- data/lib/evoasm/x64/operand.rb +156 -0
- data/lib/evoasm/x64/parameters.rb +211 -0
- data/test/helpers/population_helper.rb +128 -0
- data/test/helpers/test_helper.rb +1 -0
- data/test/helpers/x64_helper.rb +24 -0
- data/test/integration/bitwise_reverse_test.rb +41 -0
- data/test/integration/gcd_test.rb +52 -0
- data/test/integration/popcnt_test.rb +46 -0
- data/test/integration/sym_reg_test.rb +68 -0
- data/test/unit/evoasm/buffer_test.rb +48 -0
- data/test/unit/evoasm/capstone_test.rb +18 -0
- data/test/unit/evoasm/domain_test.rb +55 -0
- data/test/unit/evoasm/population/parameters_test.rb +106 -0
- data/test/unit/evoasm/population_test.rb +96 -0
- data/test/unit/evoasm/prng_test.rb +47 -0
- data/test/unit/evoasm/x64/cpu_state_test.rb +73 -0
- data/test/unit/evoasm/x64/encoding_test.rb +320 -0
- data/test/unit/evoasm/x64/instruction_access_test.rb +177 -0
- data/test/unit/evoasm/x64/instruction_encoding_test.rb +780 -0
- data/test/unit/evoasm/x64/instruction_test.rb +62 -0
- data/test/unit/evoasm/x64/parameters_test.rb +65 -0
- data/test/unit/evoasm/x64_test.rb +52 -0
- metadata +195 -89
- data/Gemfile.rake +0 -8
- data/Gemfile.rake.lock +0 -51
- data/LICENSE.txt +0 -373
- data/data/tables/README.md +0 -19
- data/data/tables/x64.csv +0 -1684
- data/data/templates/evoasm-x64.c.erb +0 -319
- data/data/templates/evoasm-x64.h.erb +0 -126
- data/examples/abs.yml +0 -20
- data/examples/popcnt.yml +0 -17
- data/examples/sym_reg.yml +0 -26
- data/exe/evoasm-search +0 -13
- data/ext/evoasm_ext/evoasm-alloc.c +0 -145
- data/ext/evoasm_ext/evoasm-alloc.h +0 -59
- data/ext/evoasm_ext/evoasm-arch.c +0 -44
- data/ext/evoasm_ext/evoasm-arch.h +0 -161
- data/ext/evoasm_ext/evoasm-bitmap.h +0 -114
- data/ext/evoasm_ext/evoasm-buf.c +0 -130
- data/ext/evoasm_ext/evoasm-buf.h +0 -47
- data/ext/evoasm_ext/evoasm-error.c +0 -31
- data/ext/evoasm_ext/evoasm-error.h +0 -75
- data/ext/evoasm_ext/evoasm-free-list.c.tmpl +0 -121
- data/ext/evoasm_ext/evoasm-free-list.h.tmpl +0 -86
- data/ext/evoasm_ext/evoasm-log.h +0 -69
- data/ext/evoasm_ext/evoasm-misc.c +0 -23
- data/ext/evoasm_ext/evoasm-misc.h +0 -282
- data/ext/evoasm_ext/evoasm-param.h +0 -37
- data/ext/evoasm_ext/evoasm-search.c +0 -2145
- data/ext/evoasm_ext/evoasm-search.h +0 -214
- data/ext/evoasm_ext/evoasm-util.h +0 -40
- data/ext/evoasm_ext/evoasm-x64.c +0 -275624
- data/ext/evoasm_ext/evoasm-x64.h +0 -5436
- data/ext/evoasm_ext/evoasm.c +0 -7
- data/ext/evoasm_ext/evoasm.h +0 -23
- data/ext/evoasm_ext/evoasm_ext.c +0 -1757
- data/ext/evoasm_ext/extconf.rb +0 -31
- data/lib/evoasm/cli.rb +0 -6
- data/lib/evoasm/cli/search.rb +0 -127
- data/lib/evoasm/core_ext.rb +0 -1
- data/lib/evoasm/core_ext/array.rb +0 -9
- data/lib/evoasm/core_ext/integer.rb +0 -10
- data/lib/evoasm/core_ext/kwstruct.rb +0 -13
- data/lib/evoasm/core_ext/range.rb +0 -5
- data/lib/evoasm/examples.rb +0 -27
- data/lib/evoasm/gen.rb +0 -8
- data/lib/evoasm/gen/enum.rb +0 -169
- data/lib/evoasm/gen/name_util.rb +0 -80
- data/lib/evoasm/gen/state.rb +0 -176
- data/lib/evoasm/gen/state_dsl.rb +0 -152
- data/lib/evoasm/gen/strio.rb +0 -27
- data/lib/evoasm/gen/translator.rb +0 -1102
- data/lib/evoasm/gen/version.rb +0 -5
- data/lib/evoasm/gen/x64.rb +0 -237
- data/lib/evoasm/gen/x64/funcs.rb +0 -495
- data/lib/evoasm/gen/x64/inst.rb +0 -781
- data/lib/evoasm/search.rb +0 -40
- data/lib/evoasm/tasks/gen_task.rb +0 -86
- data/lib/evoasm/tasks/template_task.rb +0 -52
- data/test/test_helper.rb +0 -1
- data/test/x64/test_helper.rb +0 -19
- data/test/x64/x64_test.rb +0 -87
@@ -0,0 +1,84 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2016 Julian Aron Prenner <jap@polyadic.com>
|
3
|
+
*
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
5
|
+
* it under the terms of the GNU Affero General Public License as published by
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
7
|
+
* (at your option) any later version.
|
8
|
+
*
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
* GNU Affero General Public License for more details.
|
13
|
+
*
|
14
|
+
* You should have received a copy of the GNU Affero General Public License
|
15
|
+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*/
|
17
|
+
|
18
|
+
#pragma once
|
19
|
+
|
20
|
+
#include "evoasm-util.h"
|
21
|
+
|
22
|
+
#ifdef EVOASM_UNIX
|
23
|
+
# include <unistd.h>
|
24
|
+
# include <sys/mman.h>
|
25
|
+
# if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
|
26
|
+
# define MAP_ANONYMOUS MAP_ANON
|
27
|
+
# endif
|
28
|
+
#endif
|
29
|
+
|
30
|
+
#include <string.h>
|
31
|
+
#include <alloca.h>
|
32
|
+
|
33
|
+
#if defined(_WIN32)
|
34
|
+
# include <malloc.h>
|
35
|
+
#endif
|
36
|
+
|
37
|
+
#include "evoasm-error.h"
|
38
|
+
|
39
|
+
#ifdef __GNUC__
|
40
|
+
# define EVOASM_MALLOC_ATTRS __attribute__((malloc))
|
41
|
+
# define EVOASM_CALLOC_ATTRS __attribute__((malloc))
|
42
|
+
# define EVOASM_REALLOC_ATTRS __attribute__((malloc))
|
43
|
+
# define EVOASM_ALIGNED_ALLOC_ATTRS __attribute__((malloc))
|
44
|
+
# define EVOASM_ALIGNED_CALLOC_ATTRS __attribute__((malloc))
|
45
|
+
#else
|
46
|
+
# define EVOASM_MALLOC_ATTRS
|
47
|
+
# define EVOASM_CALLOC_ATTRS
|
48
|
+
# define EVOASM_REALLOC_ATTRS
|
49
|
+
# define EVOASM_ALIGNED_ALLOC_ATTRS
|
50
|
+
# define EVOASM_ALIGNED_CALLOC_ATTRS
|
51
|
+
#endif
|
52
|
+
|
53
|
+
typedef enum {
|
54
|
+
EVOASM_MPROT_MODE_RW,
|
55
|
+
EVOASM_MPROT_MODE_RX,
|
56
|
+
EVOASM_MPROT_MODE_RWX,
|
57
|
+
} evoasm_mprot_mode_t;
|
58
|
+
|
59
|
+
void *evoasm_malloc(size_t) EVOASM_MALLOC_ATTRS;
|
60
|
+
void *evoasm_calloc(size_t, size_t) EVOASM_CALLOC_ATTRS;
|
61
|
+
void *evoasm_realloc(void *, size_t) EVOASM_REALLOC_ATTRS;
|
62
|
+
void *evoasm_aligned_alloc(size_t, size_t) EVOASM_ALIGNED_ALLOC_ATTRS;
|
63
|
+
void *evoasm_aligned_calloc(size_t align, size_t n, size_t size) EVOASM_ALIGNED_CALLOC_ATTRS;
|
64
|
+
void evoasm_free(void *);
|
65
|
+
|
66
|
+
void *evoasm_mmap(size_t size, void *p);
|
67
|
+
evoasm_success_t evoasm_munmap(void *p, size_t size);
|
68
|
+
evoasm_success_t evoasm_mprot(void *p, size_t size, evoasm_mprot_mode_t mode);
|
69
|
+
size_t evoasm_get_page_size();
|
70
|
+
|
71
|
+
#define EVOASM_TRY_ALLOC(label, func, var, ...) \
|
72
|
+
do { \
|
73
|
+
if(!(var = evoasm_##func(__VA_ARGS__))) goto label; \
|
74
|
+
} while(0);
|
75
|
+
|
76
|
+
#if defined(_WIN32)
|
77
|
+
#define evoasm_alloca(s) _malloca(s);
|
78
|
+
#else
|
79
|
+
#define evoasm_alloca(s) alloca(s);
|
80
|
+
#endif
|
81
|
+
|
82
|
+
#ifndef EVOASM_CACHE_LINE_SIZE
|
83
|
+
#define EVOASM_CACHE_LINE_SIZE 64
|
84
|
+
#endif
|
@@ -0,0 +1,52 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2016 Julian Aron Prenner <jap@polyadic.com>
|
3
|
+
*
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
5
|
+
* it under the terms of the GNU Affero General Public License as published by
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
7
|
+
* (at your option) any later version.
|
8
|
+
*
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
* GNU Affero General Public License for more details.
|
13
|
+
*
|
14
|
+
* You should have received a copy of the GNU Affero General Public License
|
15
|
+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*/
|
17
|
+
|
18
|
+
#include "evoasm-arch.h"
|
19
|
+
|
20
|
+
EVOASM_DEF_LOG_TAG("arch")
|
21
|
+
|
22
|
+
evoasm_arch_info_t _evoasm_arch_infos[] = {
|
23
|
+
{
|
24
|
+
EVOASM_ARCH_X64,
|
25
|
+
EVOASM_X64_PARAM_NONE,
|
26
|
+
15,
|
27
|
+
EVOASM_X64_INST_NONE,
|
28
|
+
0ull
|
29
|
+
}
|
30
|
+
};
|
31
|
+
|
32
|
+
evoasm_arch_id_t _evoasm_current_arch = EVOASM_ARCH_NONE;
|
33
|
+
|
34
|
+
evoasm_arch_info_t *
|
35
|
+
evoasm_get_arch_info(evoasm_arch_id_t arch_id) {
|
36
|
+
return &_evoasm_arch_infos[arch_id];
|
37
|
+
}
|
38
|
+
|
39
|
+
evoasm_arch_id_t
|
40
|
+
evoasm_get_current_arch() {
|
41
|
+
return _evoasm_current_arch;
|
42
|
+
}
|
43
|
+
|
44
|
+
#define EVOASM_ARCH_INFO_DEF_GETTER(field, type) EVOASM_DEF_GETTER(arch_info, field, type)
|
45
|
+
|
46
|
+
EVOASM_ARCH_INFO_DEF_GETTER(id, evoasm_arch_id_t)
|
47
|
+
EVOASM_ARCH_INFO_DEF_GETTER(n_params, size_t)
|
48
|
+
EVOASM_ARCH_INFO_DEF_GETTER(max_inst_len, size_t)
|
49
|
+
EVOASM_ARCH_INFO_DEF_GETTER(n_insts, size_t)
|
50
|
+
EVOASM_ARCH_INFO_DEF_GETTER(features, uint64_t)
|
51
|
+
|
52
|
+
|
@@ -0,0 +1,101 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2016 Julian Aron Prenner <jap@polyadic.com>
|
3
|
+
*
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
5
|
+
* it under the terms of the GNU Affero General Public License as published by
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
7
|
+
* (at your option) any later version.
|
8
|
+
*
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
* GNU Affero General Public License for more details.
|
13
|
+
*
|
14
|
+
* You should have received a copy of the GNU Affero General Public License
|
15
|
+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*/
|
17
|
+
|
18
|
+
#pragma once
|
19
|
+
|
20
|
+
#include <stdint.h>
|
21
|
+
#include <gen/evoasm-x64-enums.h>
|
22
|
+
|
23
|
+
#include "evoasm-error.h"
|
24
|
+
#include "evoasm-param.h"
|
25
|
+
#include "evoasm-buf.h"
|
26
|
+
#include "evoasm-log.h"
|
27
|
+
|
28
|
+
#define EVOASM_ARCH_MAX_PARAMS 32
|
29
|
+
|
30
|
+
typedef uint8_t evoasm_reg_id_t;
|
31
|
+
#define EVOASM_REG_ID_MAX UINT8_MAX
|
32
|
+
|
33
|
+
typedef uint16_t evoasm_inst_id_t;
|
34
|
+
|
35
|
+
typedef enum {
|
36
|
+
EVOASM_ARCH_X64,
|
37
|
+
EVOASM_ARCH_NONE
|
38
|
+
} evoasm_arch_id_t;
|
39
|
+
|
40
|
+
typedef struct {
|
41
|
+
uint8_t id;
|
42
|
+
uint8_t n_params;
|
43
|
+
uint8_t max_inst_len;
|
44
|
+
uint16_t n_insts;
|
45
|
+
uint64_t features;
|
46
|
+
} evoasm_arch_info_t;
|
47
|
+
|
48
|
+
|
49
|
+
typedef enum {
|
50
|
+
EVOASM_ARCH_ERROR_CODE_NOT_ENCODABLE,
|
51
|
+
EVOASM_ARCH_ERROR_CODE_MISSING_PARAM,
|
52
|
+
EVOASM_ARCH_ERROR_CODE_MISSING_FEATURE,
|
53
|
+
} evoasm_arch_error_code_t;
|
54
|
+
|
55
|
+
typedef struct {
|
56
|
+
uint8_t reg;
|
57
|
+
uint8_t param;
|
58
|
+
uint16_t inst;
|
59
|
+
} evoasm_enc_error_data_t;
|
60
|
+
|
61
|
+
typedef struct {
|
62
|
+
EVOASM_ERROR_HEADER
|
63
|
+
evoasm_enc_error_data_t data;
|
64
|
+
} evoasm_enc_error_t;
|
65
|
+
|
66
|
+
_Static_assert(sizeof(evoasm_error_data_t) >= sizeof(evoasm_enc_error_data_t), "evoasm_enc_error_data_t exceeds evoasm_error_data_t size limit");
|
67
|
+
|
68
|
+
evoasm_arch_info_t *
|
69
|
+
evoasm_get_arch_info(evoasm_arch_id_t arch_id);
|
70
|
+
|
71
|
+
evoasm_arch_id_t
|
72
|
+
evoasm_get_current_arch();
|
73
|
+
|
74
|
+
#if 0
|
75
|
+
static inline void
|
76
|
+
evoasm_enc_ctx_write_access(evoasm_buf_ref_t *arch_id, evoasm_bitmap_t *acc, evoasm_reg_id_t reg) {
|
77
|
+
evoasm_bitmap_set(acc, (size_t) reg);
|
78
|
+
}
|
79
|
+
|
80
|
+
static inline void
|
81
|
+
evoasm_enc_ctx_undefined_access(evoasm_buf_ref_t *arch_id, evoasm_bitmap_t *acc, evoasm_reg_id_t reg) {
|
82
|
+
evoasm_bitmap_unset(acc, (size_t) reg);
|
83
|
+
}
|
84
|
+
|
85
|
+
static inline evoasm_success_t
|
86
|
+
evoasm_enc_ctx_read_access_(evoasm_buf_ref_t *arch_id, evoasm_bitmap_t *acc, evoasm_reg_id_t reg,
|
87
|
+
evoasm_inst_id_t inst, const char *file, unsigned line) {
|
88
|
+
if(!evoasm_bitmap_get(acc, (size_t) reg)) {
|
89
|
+
evoasm_enc_error_data_t error_data = {
|
90
|
+
.reg = (uint8_t) reg,
|
91
|
+
.inst = (uint16_t) inst,
|
92
|
+
.arch = arch_id
|
93
|
+
};
|
94
|
+
evoasm_error(EVOASM_ERROR_TYPE_ENC, EVOASM_ENC_ERROR_CODE_INVALID_ACCESS, &error_data, file, line, "read access violation");
|
95
|
+
return false;
|
96
|
+
}
|
97
|
+
return true;
|
98
|
+
}
|
99
|
+
#define evoasm_arch_read_access(arch, acc, reg, inst) evoasm_arch_read_access_(arch, acc, reg, inst, __FILE__, __LINE__)
|
100
|
+
#endif
|
101
|
+
|
@@ -0,0 +1,158 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2016 Julian Aron Prenner <jap@polyadic.com>
|
3
|
+
*
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
5
|
+
* it under the terms of the GNU Affero General Public License as published by
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
7
|
+
* (at your option) any later version.
|
8
|
+
*
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
* GNU Affero General Public License for more details.
|
13
|
+
*
|
14
|
+
* You should have received a copy of the GNU Affero General Public License
|
15
|
+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*/
|
17
|
+
|
18
|
+
#pragma once
|
19
|
+
|
20
|
+
#include <stdint.h>
|
21
|
+
#include <stdbool.h>
|
22
|
+
#include <stdlib.h>
|
23
|
+
|
24
|
+
#define EVOASM_BITMAP_IDX_DECLS(key) \
|
25
|
+
size_t size = sizeof(uint64_t) * 8;\
|
26
|
+
size_t ary_idx = ((size_t) key) / size;\
|
27
|
+
size_t bit_idx = ((size_t) key) % size;
|
28
|
+
|
29
|
+
typedef struct {
|
30
|
+
uint64_t data[1];
|
31
|
+
} evoasm_bitmap64_t;
|
32
|
+
|
33
|
+
typedef struct {
|
34
|
+
uint64_t data[2];
|
35
|
+
} evoasm_bitmap128_t;
|
36
|
+
|
37
|
+
typedef struct {
|
38
|
+
uint64_t data[4];
|
39
|
+
} evoasm_bitmap256_t;
|
40
|
+
|
41
|
+
typedef struct {
|
42
|
+
uint64_t data[8];
|
43
|
+
} evoasm_bitmap512_t;
|
44
|
+
|
45
|
+
typedef struct {
|
46
|
+
uint64_t data[16];
|
47
|
+
} evoasm_bitmap1024_t;
|
48
|
+
|
49
|
+
typedef uint64_t evoasm_bitmap_t;
|
50
|
+
|
51
|
+
static inline void
|
52
|
+
evoasm_bitmap_set(evoasm_bitmap_t *bitmap, size_t idx) {
|
53
|
+
EVOASM_BITMAP_IDX_DECLS(idx);
|
54
|
+
bitmap[ary_idx] |= (1ull << bit_idx);
|
55
|
+
}
|
56
|
+
|
57
|
+
static inline void
|
58
|
+
evoasm_bitmap_unset(evoasm_bitmap_t *bitmap, size_t idx) {
|
59
|
+
EVOASM_BITMAP_IDX_DECLS(idx);
|
60
|
+
/* unset values must be 0*/
|
61
|
+
bitmap[ary_idx] &= ~(1ull << bit_idx);
|
62
|
+
}
|
63
|
+
|
64
|
+
static inline bool
|
65
|
+
evoasm_bitmap_get(evoasm_bitmap_t *bitmap, size_t idx) {
|
66
|
+
EVOASM_BITMAP_IDX_DECLS(idx);
|
67
|
+
return (bitmap[ary_idx] & (1ull << bit_idx)) != 0;
|
68
|
+
}
|
69
|
+
|
70
|
+
static inline void
|
71
|
+
evoasm_bitmap_set64(evoasm_bitmap_t *bitmap, size_t idx, uint64_t bits) {
|
72
|
+
EVOASM_BITMAP_IDX_DECLS(idx);
|
73
|
+
(void) bit_idx;
|
74
|
+
bitmap[ary_idx] = bits;
|
75
|
+
}
|
76
|
+
|
77
|
+
static inline void
|
78
|
+
evoasm_bitmap_or64(evoasm_bitmap_t *bitmap, size_t idx, uint64_t bits) {
|
79
|
+
EVOASM_BITMAP_IDX_DECLS(idx);
|
80
|
+
(void) bit_idx;
|
81
|
+
bitmap[ary_idx] |= bits;
|
82
|
+
}
|
83
|
+
|
84
|
+
|
85
|
+
#define EVOASM_BITMAP_DECL_IS_ZERO(width) \
|
86
|
+
static inline bool evoasm_bitmap ## width ## _is_zero(evoasm_bitmap##width##_t *bitmap) { \
|
87
|
+
size_t i;\
|
88
|
+
for(i = 0; i < width / 64; i++) {\
|
89
|
+
if(bitmap->data[i] != 0) return false;\
|
90
|
+
}\
|
91
|
+
return true;\
|
92
|
+
}
|
93
|
+
|
94
|
+
#define EVOASM_BITMAP_DECL_UNOP(name, width, op) \
|
95
|
+
static inline void evoasm_bitmap ## width ## _ ## name (evoasm_bitmap##width##_t *bitmap, evoasm_bitmap##width##_t *result) { \
|
96
|
+
size_t i;\
|
97
|
+
for(i = 0; i < width / 64; i++) {\
|
98
|
+
result->data[i] = op bitmap->data[i];\
|
99
|
+
}\
|
100
|
+
}
|
101
|
+
|
102
|
+
#define EVOASM_BITMAP_DECL_BINOP(name, width, op) \
|
103
|
+
static inline void evoasm_bitmap ## width ## _ ## name (evoasm_bitmap##width##_t *bitmap1, evoasm_bitmap##width##_t *bitmap2, evoasm_bitmap##width##_t *result) { \
|
104
|
+
size_t i;\
|
105
|
+
for(i = 0; i < width / 64; i++) {\
|
106
|
+
result->data[i] = bitmap1->data[i] op bitmap2->data[i];\
|
107
|
+
}\
|
108
|
+
}
|
109
|
+
|
110
|
+
#define EVOASM_BITMAP_DECL_EQL(width) \
|
111
|
+
static inline bool evoasm_bitmap ## width ## _ ## eql (evoasm_bitmap##width##_t *bitmap1, evoasm_bitmap##width##_t *bitmap2) { \
|
112
|
+
size_t i;\
|
113
|
+
for(i = 0; i < width / 64; i++) {\
|
114
|
+
if(bitmap1->data[i] != bitmap2->data[i]) return false;\
|
115
|
+
} \
|
116
|
+
return true;\
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
#ifdef __GNUC__
|
121
|
+
# define EVOASM_BITMAP_DECL_POPCOUNT(width) \
|
122
|
+
static inline size_t evoasm_bitmap ## width ## _ ## popcount (evoasm_bitmap##width##_t *bitmap) { \
|
123
|
+
size_t c = 0; \
|
124
|
+
size_t i;\
|
125
|
+
for(i = 0; i < width / 64; i++) {\
|
126
|
+
c += (size_t) __builtin_popcountll(bitmap->data[i]);\
|
127
|
+
} \
|
128
|
+
return c;\
|
129
|
+
}
|
130
|
+
#else
|
131
|
+
# define EVOASM_BITMAP_DECL_POPCOUNT(width) \
|
132
|
+
static inline size_t evoasm_bitmap_t ## width ## _ ## popcount (evoasm_bitmap##width##_t *bitmap) { \
|
133
|
+
size_t c = 0, i;\
|
134
|
+
for(i = 0; i < width / 64; i++) {\
|
135
|
+
uint64_t x = bitmap->data[i]; \
|
136
|
+
for(; x > 0; x &= x - 1) c++;\
|
137
|
+
} \
|
138
|
+
return c;\
|
139
|
+
}
|
140
|
+
#endif
|
141
|
+
|
142
|
+
EVOASM_BITMAP_DECL_UNOP(not, 128, ~)
|
143
|
+
EVOASM_BITMAP_DECL_BINOP(and, 128, &)
|
144
|
+
EVOASM_BITMAP_DECL_BINOP(or, 128, |)
|
145
|
+
EVOASM_BITMAP_DECL_BINOP(andn, 128, &~)
|
146
|
+
EVOASM_BITMAP_DECL_POPCOUNT(128)
|
147
|
+
EVOASM_BITMAP_DECL_EQL(128)
|
148
|
+
|
149
|
+
EVOASM_BITMAP_DECL_UNOP(not, 64, ~)
|
150
|
+
EVOASM_BITMAP_DECL_BINOP(and, 64, &)
|
151
|
+
EVOASM_BITMAP_DECL_BINOP(or, 64, |)
|
152
|
+
EVOASM_BITMAP_DECL_POPCOUNT(64)
|
153
|
+
EVOASM_BITMAP_DECL_EQL(64)
|
154
|
+
|
155
|
+
EVOASM_BITMAP_DECL_BINOP(andn, 512, &~)
|
156
|
+
EVOASM_BITMAP_DECL_IS_ZERO(512)
|
157
|
+
|
158
|
+
EVOASM_BITMAP_DECL_EQL(1024)
|
@@ -0,0 +1,204 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2016 Julian Aron Prenner <jap@polyadic.com>
|
3
|
+
*
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
5
|
+
* it under the terms of the GNU Affero General Public License as published by
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
7
|
+
* (at your option) any later version.
|
8
|
+
*
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
* GNU Affero General Public License for more details.
|
13
|
+
*
|
14
|
+
* You should have received a copy of the GNU Affero General Public License
|
15
|
+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*/
|
17
|
+
|
18
|
+
#include <string.h>
|
19
|
+
|
20
|
+
#include "evoasm-buf.h"
|
21
|
+
#include "evoasm-alloc.h"
|
22
|
+
#include "evoasm-util.h"
|
23
|
+
#include "evoasm-signal.h"
|
24
|
+
#include "evoasm.h"
|
25
|
+
|
26
|
+
EVOASM_DEF_LOG_TAG("buf")
|
27
|
+
|
28
|
+
static evoasm_success_t
|
29
|
+
evoasm_buf_init_mmap(evoasm_buf_t *buf, size_t size) {
|
30
|
+
uint8_t *mem;
|
31
|
+
|
32
|
+
//size = EVOASM_ALIGN_UP(size, evoasm_get_page_size());
|
33
|
+
mem = evoasm_mmap(size, NULL);
|
34
|
+
|
35
|
+
if(mem) {
|
36
|
+
buf->capa = size;
|
37
|
+
buf->data = mem;
|
38
|
+
buf->pos = 0;
|
39
|
+
return true;
|
40
|
+
} else {
|
41
|
+
return false;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
static evoasm_success_t
|
46
|
+
evoasm_buf_init_malloc(evoasm_buf_t *buf, size_t size) {
|
47
|
+
uint8_t *mem;
|
48
|
+
|
49
|
+
mem = malloc(size);
|
50
|
+
|
51
|
+
if(mem) {
|
52
|
+
buf->capa = size;
|
53
|
+
buf->data = mem;
|
54
|
+
buf->pos = 0;
|
55
|
+
return true;
|
56
|
+
} else {
|
57
|
+
return false;
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
evoasm_success_t
|
62
|
+
evoasm_buf_init(evoasm_buf_t *buf, evoasm_buf_type_t buf_type, size_t size) {
|
63
|
+
buf->type = buf_type;
|
64
|
+
switch(buf_type) {
|
65
|
+
case EVOASM_BUF_TYPE_MMAP:
|
66
|
+
return evoasm_buf_init_mmap(buf, size);
|
67
|
+
case EVOASM_BUF_TYPE_MALLOC:
|
68
|
+
return evoasm_buf_init_malloc(buf, size);
|
69
|
+
default:
|
70
|
+
evoasm_assert_not_reached();
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
static evoasm_success_t
|
75
|
+
evoasm_buf_destroy_mmap(evoasm_buf_t *buf) {
|
76
|
+
if(buf->data != NULL) {
|
77
|
+
return evoasm_munmap(buf->data, buf->capa);
|
78
|
+
}
|
79
|
+
return true;
|
80
|
+
}
|
81
|
+
|
82
|
+
static evoasm_success_t
|
83
|
+
evoasm_buf_destroy_malloc(evoasm_buf_t *buf) {
|
84
|
+
evoasm_free(buf->data);
|
85
|
+
return true;
|
86
|
+
}
|
87
|
+
|
88
|
+
evoasm_success_t
|
89
|
+
evoasm_buf_destroy(evoasm_buf_t *buf) {
|
90
|
+
switch(buf->type) {
|
91
|
+
case EVOASM_BUF_TYPE_MMAP:
|
92
|
+
return evoasm_buf_destroy_mmap(buf);
|
93
|
+
case EVOASM_BUF_TYPE_MALLOC:
|
94
|
+
return evoasm_buf_destroy_malloc(buf);
|
95
|
+
default:
|
96
|
+
evoasm_assert_not_reached();
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
void
|
101
|
+
evoasm_buf_reset(evoasm_buf_t *buf) {
|
102
|
+
memset(buf->data, 0, buf->pos);
|
103
|
+
buf->pos = 0;
|
104
|
+
}
|
105
|
+
|
106
|
+
evoasm_success_t
|
107
|
+
evoasm_buf_protect(evoasm_buf_t *buf, evoasm_mprot_mode_t mode) {
|
108
|
+
return evoasm_mprot(buf->data, buf->capa, mode);
|
109
|
+
}
|
110
|
+
|
111
|
+
intptr_t
|
112
|
+
evoasm_buf_exec(evoasm_buf_t *buf) {
|
113
|
+
intptr_t (*func)(void);
|
114
|
+
intptr_t result = 0;
|
115
|
+
*(void **) (&func) = buf->data;
|
116
|
+
result = func();
|
117
|
+
return result;
|
118
|
+
}
|
119
|
+
|
120
|
+
evoasm_success_t
|
121
|
+
evoasm_buf_safe_exec(evoasm_buf_t *buf, uint64_t exception_mask, intptr_t *retval) {
|
122
|
+
bool success;
|
123
|
+
|
124
|
+
evoasm_signal_set_exception_mask(exception_mask);
|
125
|
+
|
126
|
+
if(EVOASM_SIGNAL_TRY()) {
|
127
|
+
*retval = evoasm_buf_exec(buf);
|
128
|
+
success = true;
|
129
|
+
} else {
|
130
|
+
*retval = evoasm_signal_get_last_exception();
|
131
|
+
success = false;
|
132
|
+
}
|
133
|
+
|
134
|
+
evoasm_signal_clear_exception_mask();
|
135
|
+
|
136
|
+
return success;
|
137
|
+
}
|
138
|
+
|
139
|
+
void
|
140
|
+
evoasm_buf_log(evoasm_buf_t *buf, evoasm_log_level_t log_level) {
|
141
|
+
if(_evoasm_log_level > log_level) return;
|
142
|
+
|
143
|
+
evoasm_log(log_level, EVOASM_LOG_TAG, "Evoasm::Buffer: capa: %zu, pos: %zu, addr: %p\n",
|
144
|
+
buf->capa, buf->pos, (void *) buf->data);
|
145
|
+
for(size_t i = 0; i < buf->pos; i++) {
|
146
|
+
if(i > 0) evoasm_log(log_level, EVOASM_LOG_TAG, " ");
|
147
|
+
evoasm_log(log_level, EVOASM_LOG_TAG, " %02X ", buf->data[i]);
|
148
|
+
}
|
149
|
+
evoasm_log(log_level, EVOASM_LOG_TAG, " \n ");
|
150
|
+
}
|
151
|
+
|
152
|
+
void
|
153
|
+
evoasm_buf_to_buf_ref(evoasm_buf_t *buf, evoasm_buf_ref_t *buf_ref) {
|
154
|
+
buf_ref->data = buf->data;
|
155
|
+
buf_ref->pos = &buf->pos;
|
156
|
+
}
|
157
|
+
|
158
|
+
size_t
|
159
|
+
evoasm_buf_write(evoasm_buf_t *buf, uint8_t *data, size_t len) {
|
160
|
+
size_t free = buf->capa - buf->pos;
|
161
|
+
if(len > free) {
|
162
|
+
evoasm_error(EVOASM_ERROR_TYPE_BUF, EVOASM_BUF_ERROR_CODE_NO_SPACE,
|
163
|
+
"buffer does not fit (need %zu bytes but only %zu free)", len, free);
|
164
|
+
return len - free;
|
165
|
+
}
|
166
|
+
memcpy(buf->data + buf->pos, data, len);
|
167
|
+
buf->pos += len;
|
168
|
+
return 0;
|
169
|
+
}
|
170
|
+
|
171
|
+
size_t
|
172
|
+
evoasm_buf_append(evoasm_buf_t *restrict dst, evoasm_buf_t *restrict src) {
|
173
|
+
return evoasm_buf_write(dst, src->data, src->pos);
|
174
|
+
}
|
175
|
+
|
176
|
+
evoasm_success_t
|
177
|
+
evoasm_buf_clone(evoasm_buf_t *restrict buf, evoasm_buf_t *restrict cloned_buf) {
|
178
|
+
if(!evoasm_buf_init(cloned_buf, (evoasm_buf_type_t) buf->type, buf->capa)) {
|
179
|
+
return false;
|
180
|
+
}
|
181
|
+
return evoasm_buf_append(cloned_buf, buf) == 0;
|
182
|
+
}
|
183
|
+
|
184
|
+
uint8_t *
|
185
|
+
evoasm_buf_data(evoasm_buf_t *buf) {
|
186
|
+
return buf->data;
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
EVOASM_DEF_ALLOC_FREE_FUNCS(buf_ref)
|
191
|
+
|
192
|
+
EVOASM_DEF_ALLOC_FREE_FUNCS(buf)
|
193
|
+
|
194
|
+
EVOASM_DEF_GETTER(buf, capa, size_t)
|
195
|
+
EVOASM_DEF_GETTER(buf, pos, size_t)
|
196
|
+
EVOASM_DEF_GETTER(buf, type, evoasm_buf_type_t)
|
197
|
+
EVOASM_DEF_GETTER(buf, data, const uint8_t *)
|
198
|
+
|
199
|
+
void
|
200
|
+
evoasm_buf_ref_init(evoasm_buf_ref_t *buf_ref, uint8_t *data, size_t *pos) {
|
201
|
+
buf_ref->data = data;
|
202
|
+
buf_ref->pos = pos;
|
203
|
+
}
|
204
|
+
|