2 * Copyright 2020 EfficiOS, Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include "common/align.h"
14 #define INIT_ALLOC_SIZE 4
16 static inline int get_count_order(unsigned int count
)
20 order
= lttng_fls(count
) - 1;
21 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
;
73 int bytecode_push(struct lttng_bytecode_alloc
**fb
, const void *data
,
74 uint32_t align
, uint32_t len
)
78 offset
= bytecode_reserve(fb
, align
, len
);
81 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
86 int bytecode_push_logical(struct lttng_bytecode_alloc
**fb
,
87 struct logical_op
*data
,
88 uint32_t align
, uint32_t len
,
89 uint16_t *skip_offset
)
93 offset
= bytecode_reserve(fb
, align
, len
);
96 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
98 (void *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
99 - (void *) &(*fb
)->b
.data
[0];
104 int bytecode_push_get_payload_root(struct lttng_bytecode_alloc
**bytecode
)
106 struct load_op
*insn
;
107 uint32_t insn_len
= sizeof(struct load_op
);
110 insn
= calloc(insn_len
, 1);
114 insn
->op
= BYTECODE_OP_GET_PAYLOAD_ROOT
;
115 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
122 int bytecode_push_get_context_root(struct lttng_bytecode_alloc
**bytecode
)
124 struct load_op
*insn
;
125 uint32_t insn_len
= sizeof(struct load_op
);
128 insn
= calloc(insn_len
, 1);
132 insn
->op
= BYTECODE_OP_GET_CONTEXT_ROOT
;
133 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
140 int bytecode_push_get_app_context_root(struct lttng_bytecode_alloc
**bytecode
)
142 struct load_op
*insn
;
143 uint32_t insn_len
= sizeof(struct load_op
);
146 insn
= calloc(insn_len
, 1);
150 insn
->op
= BYTECODE_OP_GET_APP_CONTEXT_ROOT
;
151 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
158 int bytecode_push_get_index_u64(struct lttng_bytecode_alloc
**bytecode
,
161 struct load_op
*insn
;
162 uint32_t insn_len
= sizeof(struct load_op
)
163 + sizeof(struct get_index_u64
);
164 struct get_index_u64 index_op_data
;
167 insn
= calloc(insn_len
, 1);
171 insn
->op
= BYTECODE_OP_GET_INDEX_U64
;
172 index_op_data
.index
= index
;
173 memcpy(insn
->data
, &index_op_data
, sizeof(index
));
174 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
182 int bytecode_push_get_symbol(struct lttng_bytecode_alloc
**bytecode
,
183 struct lttng_bytecode_alloc
**bytecode_reloc
,
186 struct load_op
*insn
;
187 uint32_t insn_len
= sizeof(struct load_op
)
188 + sizeof(struct get_symbol
);
189 struct get_symbol symbol_offset
;
190 uint32_t reloc_offset_u32
;
191 uint16_t reloc_offset
;
192 uint32_t bytecode_reloc_offset_u32
;
195 insn
= calloc(insn_len
, 1);
201 insn
->op
= BYTECODE_OP_GET_SYMBOL
;
204 * Get offset in the reloc portion at which the symbol name
205 * will end up at (GET_SYMBOL's operand points there).
207 bytecode_reloc_offset_u32
=
208 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
*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
);