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
)
107 struct load_op
*insn
;
108 const uint32_t insn_len
= sizeof(struct load_op
);
110 insn
= calloc(insn_len
, 1);
116 insn
->op
= BYTECODE_OP_GET_PAYLOAD_ROOT
;
117 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
124 int bytecode_push_get_context_root(struct lttng_bytecode_alloc
**bytecode
)
127 struct load_op
*insn
;
128 const uint32_t insn_len
= sizeof(struct load_op
);
130 insn
= calloc(insn_len
, 1);
136 insn
->op
= BYTECODE_OP_GET_CONTEXT_ROOT
;
137 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
144 int bytecode_push_get_app_context_root(struct lttng_bytecode_alloc
**bytecode
)
147 struct load_op
*insn
;
148 const uint32_t insn_len
= sizeof(struct load_op
);
150 insn
= calloc(insn_len
, 1);
156 insn
->op
= BYTECODE_OP_GET_APP_CONTEXT_ROOT
;
157 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
164 int bytecode_push_get_index_u64(struct lttng_bytecode_alloc
**bytecode
,
168 struct load_op
*insn
;
169 struct get_index_u64 index_op_data
;
170 const uint32_t insn_len
=
171 sizeof(struct load_op
) + sizeof(struct get_index_u64
);
173 insn
= calloc(insn_len
, 1);
179 insn
->op
= BYTECODE_OP_GET_INDEX_U64
;
180 index_op_data
.index
= index
;
181 memcpy(insn
->data
, &index_op_data
, sizeof(index
));
182 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
190 int bytecode_push_get_symbol(struct lttng_bytecode_alloc
**bytecode
,
191 struct lttng_bytecode_alloc
**bytecode_reloc
,
195 struct load_op
*insn
;
196 struct get_symbol symbol_offset
;
197 uint32_t reloc_offset_u32
;
198 uint16_t reloc_offset
;
199 uint32_t bytecode_reloc_offset_u32
;
200 const uint32_t insn_len
=
201 sizeof(struct load_op
) + sizeof(struct get_symbol
);
203 insn
= calloc(insn_len
, 1);
209 insn
->op
= BYTECODE_OP_GET_SYMBOL
;
212 * Get offset in the reloc portion at which the symbol name
213 * will end up at (GET_SYMBOL's operand points there).
215 bytecode_reloc_offset_u32
= bytecode_get_len(&(*bytecode_reloc
)->b
) +
216 sizeof(reloc_offset
);
217 symbol_offset
.offset
= (uint16_t) bytecode_reloc_offset_u32
;
218 memcpy(insn
->data
, &symbol_offset
, sizeof(symbol_offset
));
221 * Get offset in the bytecode where the opcode will end up at,
222 * the reloc offset points to it.
224 reloc_offset_u32
= bytecode_get_len(&(*bytecode
)->b
);
225 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
229 reloc_offset
= (uint16_t) reloc_offset_u32
;
231 /* Append op in bytecode. */
232 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
237 /* Append reloc offset. */
238 ret
= bytecode_push(bytecode_reloc
, &reloc_offset
,
239 1, sizeof(reloc_offset
));
244 /* Append symbol name. */
245 ret
= bytecode_push(bytecode_reloc
, symbol
, 1, strlen(symbol
) + 1);
253 * Allocate an lttng_bytecode object and copy the given original bytecode.
255 * Return allocated bytecode or NULL on error.
258 struct lttng_bytecode
*lttng_bytecode_copy(
259 const struct lttng_bytecode
*orig_f
)
261 struct lttng_bytecode
*bytecode
= NULL
;
263 bytecode
= zmalloc(sizeof(*bytecode
) + orig_f
->len
);
268 memcpy(bytecode
, orig_f
, sizeof(*bytecode
) + orig_f
->len
);