2 * Copyright 2020 EfficiOS, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include "bytecode.hpp"
9 #include "common/align.hpp"
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))
26 int bytecode_init(struct lttng_bytecode_alloc
**fb
)
30 alloc_len
= sizeof(struct lttng_bytecode_alloc
) + INIT_ALLOC_SIZE
;
31 *fb
= (lttng_bytecode_alloc
*) calloc(alloc_len
, 1);
35 (*fb
)->alloc_len
= alloc_len
;
40 static int32_t bytecode_reserve(struct lttng_bytecode_alloc
**fb
, uint32_t align
, uint32_t len
)
43 uint32_t padding
= lttng_offset_align((*fb
)->b
.len
, align
);
44 uint32_t new_len
= (*fb
)->b
.len
+ padding
+ len
;
45 uint32_t new_alloc_len
= sizeof(struct lttng_bytecode_alloc
) + new_len
;
46 uint32_t old_alloc_len
= (*fb
)->alloc_len
;
48 if (new_len
> LTTNG_FILTER_MAX_LEN
)
51 if (new_alloc_len
> old_alloc_len
) {
52 struct lttng_bytecode_alloc
*newptr
;
54 new_alloc_len
= std::max(1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
55 newptr
= (lttng_bytecode_alloc
*) realloc(*fb
, new_alloc_len
);
59 /* We zero directly the memory from start of allocation. */
60 memset(&((char *) *fb
)[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
61 (*fb
)->alloc_len
= new_alloc_len
;
63 (*fb
)->b
.len
+= padding
;
69 int bytecode_push(struct lttng_bytecode_alloc
**fb
, const void *data
, uint32_t align
, uint32_t len
)
73 offset
= bytecode_reserve(fb
, align
, len
);
76 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
80 int bytecode_push_logical(struct lttng_bytecode_alloc
**fb
,
81 struct logical_op
*data
,
84 uint16_t *skip_offset
)
88 offset
= bytecode_reserve(fb
, align
, len
);
91 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
92 *skip_offset
= (char *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
-
93 (char *) &(*fb
)->b
.data
[0];
97 int bytecode_push_get_payload_root(struct lttng_bytecode_alloc
**bytecode
)
100 struct load_op
*insn
;
101 const uint32_t insn_len
= sizeof(struct load_op
);
103 insn
= (load_op
*) calloc(insn_len
, 1);
109 insn
->op
= BYTECODE_OP_GET_PAYLOAD_ROOT
;
110 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
116 int bytecode_push_get_context_root(struct lttng_bytecode_alloc
**bytecode
)
119 struct load_op
*insn
;
120 const uint32_t insn_len
= sizeof(struct load_op
);
122 insn
= (load_op
*) calloc(insn_len
, 1);
128 insn
->op
= BYTECODE_OP_GET_CONTEXT_ROOT
;
129 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
135 int bytecode_push_get_app_context_root(struct lttng_bytecode_alloc
**bytecode
)
138 struct load_op
*insn
;
139 const uint32_t insn_len
= sizeof(struct load_op
);
141 insn
= (load_op
*) calloc(insn_len
, 1);
147 insn
->op
= BYTECODE_OP_GET_APP_CONTEXT_ROOT
;
148 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
154 int bytecode_push_get_index_u64(struct lttng_bytecode_alloc
**bytecode
, uint64_t index
)
157 struct load_op
*insn
;
158 struct get_index_u64 index_op_data
;
159 const uint32_t insn_len
= sizeof(struct load_op
) + sizeof(struct get_index_u64
);
161 insn
= (load_op
*) calloc(insn_len
, 1);
167 insn
->op
= BYTECODE_OP_GET_INDEX_U64
;
168 index_op_data
.index
= index
;
169 memcpy(insn
->data
, &index_op_data
, sizeof(index
));
170 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
177 int bytecode_push_get_symbol(struct lttng_bytecode_alloc
**bytecode
,
178 struct lttng_bytecode_alloc
**bytecode_reloc
,
182 struct load_op
*insn
;
183 struct get_symbol symbol_offset
;
184 uint32_t reloc_offset_u32
;
185 uint16_t reloc_offset
;
186 uint32_t bytecode_reloc_offset_u32
;
187 const uint32_t insn_len
= sizeof(struct load_op
) + sizeof(struct get_symbol
);
189 insn
= (load_op
*) calloc(insn_len
, 1);
195 insn
->op
= BYTECODE_OP_GET_SYMBOL
;
198 * Get offset in the reloc portion at which the symbol name
199 * will end up at (GET_SYMBOL's operand points there).
201 bytecode_reloc_offset_u32
= bytecode_get_len(&(*bytecode_reloc
)->b
) + sizeof(reloc_offset
);
202 symbol_offset
.offset
= (uint16_t) bytecode_reloc_offset_u32
;
203 memcpy(insn
->data
, &symbol_offset
, sizeof(symbol_offset
));
206 * Get offset in the bytecode where the opcode will end up at,
207 * the reloc offset points to it.
209 reloc_offset_u32
= bytecode_get_len(&(*bytecode
)->b
);
210 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
214 reloc_offset
= (uint16_t) reloc_offset_u32
;
216 /* Append op in bytecode. */
217 ret
= bytecode_push(bytecode
, insn
, 1, insn_len
);
222 /* Append reloc offset. */
223 ret
= bytecode_push(bytecode_reloc
, &reloc_offset
, 1, sizeof(reloc_offset
));
228 /* Append symbol name. */
229 ret
= bytecode_push(bytecode_reloc
, symbol
, 1, strlen(symbol
) + 1);
237 * Allocate an lttng_bytecode object and copy the given original bytecode.
239 * Return allocated bytecode or NULL on error.
241 struct lttng_bytecode
*lttng_bytecode_copy(const struct lttng_bytecode
*orig_f
)
243 lttng_bytecode
*bytecode
= zmalloc
<lttng_bytecode
>(sizeof(*bytecode
) + orig_f
->len
);
248 memcpy(bytecode
, orig_f
, sizeof(*bytecode
) + orig_f
->len
);