2 * Copyright 2020 EfficiOS, Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include "common/align.h"
14 #define INIT_ALLOC_SIZE 4
17 int get_count_order(unsigned int count
)
21 order
= lttng_fls(count
) - 1;
22 if (count
& (count
- 1))
27 int bytecode_init(struct lttng_bytecode_alloc
**fb
)
31 alloc_len
= sizeof(struct lttng_bytecode_alloc
) + INIT_ALLOC_SIZE
;
32 *fb
= calloc(alloc_len
, 1);
36 (*fb
)->alloc_len
= alloc_len
;
42 int32_t bytecode_reserve(struct lttng_bytecode_alloc
**fb
, uint32_t align
, uint32_t len
)
45 uint32_t padding
= offset_align((*fb
)->b
.len
, align
);
46 uint32_t new_len
= (*fb
)->b
.len
+ padding
+ len
;
47 uint32_t new_alloc_len
= sizeof(struct lttng_bytecode_alloc
) + new_len
;
48 uint32_t old_alloc_len
= (*fb
)->alloc_len
;
50 if (new_len
> LTTNG_FILTER_MAX_LEN
)
53 if (new_alloc_len
> old_alloc_len
) {
54 struct lttng_bytecode_alloc
*newptr
;
57 max_t(uint32_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
58 newptr
= realloc(*fb
, new_alloc_len
);
62 /* We zero directly the memory from start of allocation. */
63 memset(&((char *) *fb
)[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
64 (*fb
)->alloc_len
= new_alloc_len
;
66 (*fb
)->b
.len
+= padding
;
72 int bytecode_push(struct lttng_bytecode_alloc
**fb
, const void *data
,
73 uint32_t align
, uint32_t len
)
77 offset
= bytecode_reserve(fb
, align
, len
);
80 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
84 int bytecode_push_logical(struct lttng_bytecode_alloc
**fb
,
85 struct logical_op
*data
,
86 uint32_t align
, uint32_t len
,
87 uint16_t *skip_offset
)
91 offset
= bytecode_reserve(fb
, align
, len
);
94 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
96 (void *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
97 - (void *) &(*fb
)->b
.data
[0];
101 int bytecode_push_get_payload_root(struct lttng_bytecode_alloc
**bytecode
)
104 struct load_op
*insn
;
105 const uint32_t insn_len
= sizeof(struct load_op
);
107 insn
= calloc(insn_len
, 1);
113 insn
->op
= BYTECODE_OP_GET_PAYLOAD_ROOT
;
114 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
120 int bytecode_push_get_context_root(struct lttng_bytecode_alloc
**bytecode
)
123 struct load_op
*insn
;
124 const uint32_t insn_len
= sizeof(struct load_op
);
126 insn
= calloc(insn_len
, 1);
132 insn
->op
= BYTECODE_OP_GET_CONTEXT_ROOT
;
133 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
139 int bytecode_push_get_app_context_root(struct lttng_bytecode_alloc
**bytecode
)
142 struct load_op
*insn
;
143 const uint32_t insn_len
= sizeof(struct load_op
);
145 insn
= calloc(insn_len
, 1);
151 insn
->op
= BYTECODE_OP_GET_APP_CONTEXT_ROOT
;
152 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
158 int bytecode_push_get_index_u64(struct lttng_bytecode_alloc
**bytecode
,
162 struct load_op
*insn
;
163 struct get_index_u64 index_op_data
;
164 const uint32_t insn_len
=
165 sizeof(struct load_op
) + sizeof(struct get_index_u64
);
167 insn
= calloc(insn_len
, 1);
173 insn
->op
= BYTECODE_OP_GET_INDEX_U64
;
174 index_op_data
.index
= index
;
175 memcpy(insn
->data
, &index_op_data
, sizeof(index
));
176 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
183 int bytecode_push_get_symbol(struct lttng_bytecode_alloc
**bytecode
,
184 struct lttng_bytecode_alloc
**bytecode_reloc
,
188 struct load_op
*insn
;
189 struct get_symbol symbol_offset
;
190 uint32_t reloc_offset_u32
;
191 uint16_t reloc_offset
;
192 uint32_t bytecode_reloc_offset_u32
;
193 const uint32_t insn_len
=
194 sizeof(struct load_op
) + sizeof(struct get_symbol
);
196 insn
= calloc(insn_len
, 1);
202 insn
->op
= BYTECODE_OP_GET_SYMBOL
;
205 * Get offset in the reloc portion at which the symbol name
206 * will end up at (GET_SYMBOL's operand points there).
208 bytecode_reloc_offset_u32
= bytecode_get_len(&(*bytecode_reloc
)->b
) +
209 sizeof(reloc_offset
);
210 symbol_offset
.offset
= (uint16_t) bytecode_reloc_offset_u32
;
211 memcpy(insn
->data
, &symbol_offset
, sizeof(symbol_offset
));
214 * Get offset in the bytecode where the opcode will end up at,
215 * the reloc offset points to it.
217 reloc_offset_u32
= bytecode_get_len(&(*bytecode
)->b
);
218 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
222 reloc_offset
= (uint16_t) reloc_offset_u32
;
224 /* Append op in bytecode. */
225 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
230 /* Append reloc offset. */
231 ret
= bytecode_push(bytecode_reloc
, &reloc_offset
,
232 1, sizeof(reloc_offset
));
237 /* Append symbol name. */
238 ret
= bytecode_push(bytecode_reloc
, symbol
, 1, strlen(symbol
) + 1);
246 * Allocate an lttng_bytecode object and copy the given original bytecode.
248 * Return allocated bytecode or NULL on error.
250 struct lttng_bytecode
*lttng_bytecode_copy(
251 const struct lttng_bytecode
*orig_f
)
253 struct lttng_bytecode
*bytecode
= NULL
;
255 bytecode
= zmalloc(sizeof(*bytecode
) + orig_f
->len
);
260 memcpy(bytecode
, orig_f
, sizeof(*bytecode
) + orig_f
->len
);