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))
28 int bytecode_init(struct lttng_bytecode_alloc
**fb
)
32 alloc_len
= sizeof(struct lttng_bytecode_alloc
) + INIT_ALLOC_SIZE
;
33 *fb
= calloc(alloc_len
, 1);
37 (*fb
)->alloc_len
= alloc_len
;
43 int32_t bytecode_reserve(struct lttng_bytecode_alloc
**fb
, uint32_t align
, uint32_t len
)
46 uint32_t padding
= offset_align((*fb
)->b
.len
, align
);
47 uint32_t new_len
= (*fb
)->b
.len
+ padding
+ len
;
48 uint32_t new_alloc_len
= sizeof(struct lttng_bytecode_alloc
) + new_len
;
49 uint32_t old_alloc_len
= (*fb
)->alloc_len
;
51 if (new_len
> LTTNG_FILTER_MAX_LEN
)
54 if (new_alloc_len
> old_alloc_len
) {
55 struct lttng_bytecode_alloc
*newptr
;
58 max_t(uint32_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
59 newptr
= realloc(*fb
, new_alloc_len
);
63 /* We zero directly the memory from start of allocation. */
64 memset(&((char *) *fb
)[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
65 (*fb
)->alloc_len
= new_alloc_len
;
67 (*fb
)->b
.len
+= padding
;
74 int bytecode_push(struct lttng_bytecode_alloc
**fb
, const void *data
,
75 uint32_t align
, uint32_t len
)
79 offset
= bytecode_reserve(fb
, align
, len
);
82 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
87 int bytecode_push_logical(struct lttng_bytecode_alloc
**fb
,
88 struct logical_op
*data
,
89 uint32_t align
, uint32_t len
,
90 uint16_t *skip_offset
)
94 offset
= bytecode_reserve(fb
, align
, len
);
97 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
99 (void *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
100 - (void *) &(*fb
)->b
.data
[0];
105 int bytecode_push_get_payload_root(struct lttng_bytecode_alloc
**bytecode
)
108 struct load_op
*insn
;
109 const uint32_t insn_len
= sizeof(struct load_op
);
111 insn
= calloc(insn_len
, 1);
117 insn
->op
= BYTECODE_OP_GET_PAYLOAD_ROOT
;
118 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
125 int bytecode_push_get_context_root(struct lttng_bytecode_alloc
**bytecode
)
128 struct load_op
*insn
;
129 const uint32_t insn_len
= sizeof(struct load_op
);
131 insn
= calloc(insn_len
, 1);
137 insn
->op
= BYTECODE_OP_GET_CONTEXT_ROOT
;
138 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
145 int bytecode_push_get_app_context_root(struct lttng_bytecode_alloc
**bytecode
)
148 struct load_op
*insn
;
149 const uint32_t insn_len
= sizeof(struct load_op
);
151 insn
= calloc(insn_len
, 1);
157 insn
->op
= BYTECODE_OP_GET_APP_CONTEXT_ROOT
;
158 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
165 int bytecode_push_get_index_u64(struct lttng_bytecode_alloc
**bytecode
,
169 struct load_op
*insn
;
170 struct get_index_u64 index_op_data
;
171 const uint32_t insn_len
=
172 sizeof(struct load_op
) + sizeof(struct get_index_u64
);
174 insn
= calloc(insn_len
, 1);
180 insn
->op
= BYTECODE_OP_GET_INDEX_U64
;
181 index_op_data
.index
= index
;
182 memcpy(insn
->data
, &index_op_data
, sizeof(index
));
183 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
191 int bytecode_push_get_symbol(struct lttng_bytecode_alloc
**bytecode
,
192 struct lttng_bytecode_alloc
**bytecode_reloc
,
196 struct load_op
*insn
;
197 struct get_symbol symbol_offset
;
198 uint32_t reloc_offset_u32
;
199 uint16_t reloc_offset
;
200 uint32_t bytecode_reloc_offset_u32
;
201 const uint32_t insn_len
=
202 sizeof(struct load_op
) + sizeof(struct get_symbol
);
204 insn
= calloc(insn_len
, 1);
210 insn
->op
= BYTECODE_OP_GET_SYMBOL
;
213 * Get offset in the reloc portion at which the symbol name
214 * will end up at (GET_SYMBOL's operand points there).
216 bytecode_reloc_offset_u32
= bytecode_get_len(&(*bytecode_reloc
)->b
) +
217 sizeof(reloc_offset
);
218 symbol_offset
.offset
= (uint16_t) bytecode_reloc_offset_u32
;
219 memcpy(insn
->data
, &symbol_offset
, sizeof(symbol_offset
));
222 * Get offset in the bytecode where the opcode will end up at,
223 * the reloc offset points to it.
225 reloc_offset_u32
= bytecode_get_len(&(*bytecode
)->b
);
226 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
230 reloc_offset
= (uint16_t) reloc_offset_u32
;
232 /* Append op in bytecode. */
233 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
238 /* Append reloc offset. */
239 ret
= bytecode_push(bytecode_reloc
, &reloc_offset
,
240 1, sizeof(reloc_offset
));
245 /* Append symbol name. */
246 ret
= bytecode_push(bytecode_reloc
, symbol
, 1, strlen(symbol
) + 1);
254 * Allocate an lttng_bytecode object and copy the given original bytecode.
256 * Return allocated bytecode or NULL on error.
259 struct lttng_bytecode
*lttng_bytecode_copy(
260 const struct lttng_bytecode
*orig_f
)
262 struct lttng_bytecode
*bytecode
= NULL
;
264 bytecode
= zmalloc(sizeof(*bytecode
) + orig_f
->len
);
269 memcpy(bytecode
, orig_f
, sizeof(*bytecode
) + orig_f
->len
);