summaryrefslogtreecommitdiffhomepage
path: root/program.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-07-14 23:15:11 +0200
committerJo-Philipp Wich <jo@mein.io>2022-07-30 00:41:56 +0200
commit9c9a9ec383608287802bb2639a4ee6b7bbfd6793 (patch)
tree531d3c957ffdc5b91f5afee5a203a7fe64a6982f /program.c
parent41114a02a38a65956010bab95c4bff19af7ac1ed (diff)
program: fix en/decoding debuginfo upvalue slots in precompiled bytecode
The sizeof(size_t) might differ from the sizeof(uint32_t) used to serialize compiled bytecode, so extra care is needed to properly encode and decode upvalue slot values which are defined as (size_t)-1 / 2 + n. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'program.c')
-rw-r--r--program.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/program.c b/program.c
index 1810b06..bfe50bc 100644
--- a/program.c
+++ b/program.c
@@ -224,7 +224,7 @@ enum {
static void
write_chunk(uc_chunk_t *chunk, FILE *file, uint32_t flags)
{
- size_t i;
+ size_t i, slot;
/* write bytecode data */
write_vector(chunk, file);
@@ -246,9 +246,14 @@ write_chunk(uc_chunk_t *chunk, FILE *file, uint32_t flags)
write_u32(chunk->debuginfo.variables.count, file);
for (i = 0; i < chunk->debuginfo.variables.count; i++) {
+ slot = chunk->debuginfo.variables.entries[i].slot;
+
+ if (slot >= ((size_t)-1 / 2))
+ slot = ((uint32_t)-1 / 2) + (slot - ((size_t)-1 / 2));
+
write_u32(chunk->debuginfo.variables.entries[i].from, file);
write_u32(chunk->debuginfo.variables.entries[i].to, file);
- write_u32(chunk->debuginfo.variables.entries[i].slot, file);
+ write_u32(slot, file);
write_u32(chunk->debuginfo.variables.entries[i].nameidx, file);
}
@@ -657,6 +662,9 @@ read_chunk(FILE *file, uc_chunk_t *chunk, uint32_t flags, const char *subj, char
!read_size_t(file, &varrange->slot, sizeof(uint32_t), subjbuf, errp) ||
!read_size_t(file, &varrange->nameidx, sizeof(uint32_t), subjbuf, errp))
goto out;
+
+ if (varrange->slot >= ((uint32_t)-1 / 2))
+ varrange->slot = ((size_t)-1 / 2) + (varrange->slot - ((uint32_t)-1 / 2));
}
snprintf(subjbuf, sizeof(subjbuf), "%s variable names", subj);