Skip to content

Commit f7e8f71

Browse files
committed
Print an error message when trying to use OTP-28 code on v0.6.x
Avoid any kind of frustrating and hard to debug crash. Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent 99a9e91 commit f7e8f71

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/libAtomVM/module.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
return; \
5050
}
5151

52+
static bool module_are_literals_compressed(const uint8_t *litT);
5253
#ifdef WITH_ZLIB
5354
static void *module_uncompress_literals(const uint8_t *litT, int size);
5455
#endif
@@ -72,6 +73,13 @@ static enum ModuleLoadResult module_populate_atoms_table(Module *this_module, ui
7273
{
7374
int atoms_count = READ_32_UNALIGNED(table_data + 8);
7475

76+
if (UNLIKELY(atoms_count < 0)) {
77+
fprintf(stderr, "Code compiled with OTP-28 is not supported by this version of AtomVM.\n"
78+
"Please recompile your code using an earlier version, such as OTP-27,\n"
79+
"or switch to a newer version of AtomVM, such as a main snapshot.\n");
80+
AVM_ABORT();
81+
}
82+
7583
const char *current_atom = (const char *) table_data + 12;
7684

7785
this_module->local_atoms_to_global_table = calloc(atoms_count + 1, sizeof(int));
@@ -284,6 +292,11 @@ Module *module_new_from_iff_binary(GlobalContext *global, const void *iff_binary
284292
module_parse_line_table(mod, beam_file + offsets[LINT] + 8, sizes[LINT]);
285293

286294
if (offsets[LITT]) {
295+
if (UNLIKELY(!module_are_literals_compressed(beam_file + offsets[LITT]))) {
296+
fprintf(stderr, "Code compiled with OTP-28 is not supported by this version of AtomVM.\n"
297+
"Please recompile your code using an earlier version, such as OTP-27,\n"
298+
"or switch to a newer version of AtomVM, such as a main snapshot.\n");
299+
}
287300
#ifdef WITH_ZLIB
288301
mod->literals_data = module_uncompress_literals(beam_file + offsets[LITT], sizes[LITT]);
289302
if (IS_NULL_PTR(mod->literals_data)) {
@@ -364,6 +377,12 @@ COLD_FUNC void module_destroy(Module *module)
364377
free(module);
365378
}
366379

380+
static bool module_are_literals_compressed(const uint8_t *litT)
381+
{
382+
uint32_t required_buf_size = READ_32_ALIGNED(litT + LITT_UNCOMPRESSED_SIZE_OFFSET);
383+
return (required_buf_size != 0);
384+
}
385+
367386
#ifdef WITH_ZLIB
368387
static void *module_uncompress_literals(const uint8_t *litT, int size)
369388
{

tools/packbeam/packbeam.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct FileData {
4747
} FileData;
4848

4949
static void pad_and_align(FILE *f);
50+
bool are_literals_compressed(const uint8_t *litT);
5051
static void *uncompress_literals(const uint8_t *litT, int size, size_t *uncompressedSize);
5152
static void add_module_header(FILE *f, const char *module_name, uint32_t flags);
5253
static void pack_beam_file(FILE *pack, const uint8_t *data, size_t size, const char *filename, int is_entrypoint, bool include_lines);
@@ -340,6 +341,12 @@ static void pack_beam_file(FILE *pack, const uint8_t *data, size_t size, const c
340341
}
341342

342343
if (offsets[LITT]) {
344+
if (UNLIKELY(!are_literals_compressed(data + offsets[LITT]))) {
345+
fprintf(stderr, "Code compiled with OTP-28 is not supported by this version of AtomVM.\n"
346+
"Please recompile using an earlier version, such as OTP-27,\n"
347+
"or switch to a newer version of AtomVM, such as a main snapshot.\n");
348+
abort();
349+
}
343350
size_t u_size;
344351
void *deflated = uncompress_literals(data + offsets[LITT], sizes[LITT], &u_size);
345352
assert_fwrite("LitU", 4, pack);
@@ -419,6 +426,12 @@ static int do_list(int argc, char **argv)
419426
return ret;
420427
}
421428

429+
bool are_literals_compressed(const uint8_t *litT)
430+
{
431+
unsigned int required_buf_size = READ_32_ALIGNED(litT + LITT_UNCOMPRESSED_SIZE_OFFSET);
432+
return (required_buf_size != 0);
433+
}
434+
422435
static void *uncompress_literals(const uint8_t *litT, int size, size_t *uncompressedSize)
423436
{
424437
unsigned int required_buf_size = READ_32_ALIGNED(litT + LITT_UNCOMPRESSED_SIZE_OFFSET);

0 commit comments

Comments
 (0)