Skip to content

Commit bf92bfd

Browse files
committed
Forward port changes from v0.6 release branch
Merge fix from release-0.6 for unaligned memory access when loading modules from binaries (see also #1634).
2 parents c4bd30a + 95ac53c commit bf92bfd

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

.github/workflows/build-and-test-other.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ jobs:
9494
9595
- arch: "arm32v7"
9696
platform: "arm/v7"
97-
tag: "bullseye"
97+
tag: "bookworm"
9898
# -D_FILE_OFFSET_BITS=64 is required for making atomvm:posix_readdir/1 test work
9999
# otherwise readdir will fail due to 64 bits inode numbers with 32 bit ino_t
100100
cflags: "-mcpu=cortex-a7 -mfloat-abi=hard -O2 -mthumb -mthumb-interwork -D_FILE_OFFSET_BITS=64"

src/libAtomVM/iff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void scan_iff(const void *iff_binary, int buf_size, unsigned long *offsets, unsi
5050

5151
int current_pos = 12;
5252

53-
uint32_t iff_size = READ_32_ALIGNED(data + 4);
53+
uint32_t iff_size = READ_32_UNALIGNED(data + 4);
5454
int file_size = iff_size;
5555
if (UNLIKELY(buf_size < file_size)) {
5656
fprintf(stderr, "error: buffer holding IFF is smaller than IFF size: %i\n", buf_size);

src/libAtomVM/module.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static void module_parse_line_table(Module *mod, const uint8_t *data, size_t len
6565

6666
static enum ModuleLoadResult module_populate_atoms_table(Module *this_module, uint8_t *table_data, GlobalContext *glb)
6767
{
68-
int atoms_count = READ_32_ALIGNED(table_data + 8);
68+
int atoms_count = READ_32_UNALIGNED(table_data + 8);
6969

7070
enum EnsureAtomsOpt ensure_opts = EnsureAtomsNoOpts;
7171
if (atoms_count < 0) {
@@ -95,7 +95,7 @@ static enum ModuleLoadResult module_populate_atoms_table(Module *this_module, ui
9595

9696
static enum ModuleLoadResult module_build_imported_functions_table(Module *this_module, uint8_t *table_data, GlobalContext *glb)
9797
{
98-
int functions_count = READ_32_ALIGNED(table_data + 8);
98+
int functions_count = READ_32_UNALIGNED(table_data + 8);
9999

100100
this_module->imported_funcs = calloc(functions_count, sizeof(struct ExportedFunction *));
101101
if (IS_NULL_PTR(this_module->imported_funcs)) {
@@ -104,11 +104,11 @@ static enum ModuleLoadResult module_build_imported_functions_table(Module *this_
104104
}
105105

106106
for (int i = 0; i < functions_count; i++) {
107-
int local_module_atom_index = READ_32_ALIGNED(table_data + i * 12 + 12);
108-
int local_function_atom_index = READ_32_ALIGNED(table_data + i * 12 + 4 + 12);
107+
int local_module_atom_index = READ_32_UNALIGNED(table_data + i * 12 + 12);
108+
int local_function_atom_index = READ_32_UNALIGNED(table_data + i * 12 + 4 + 12);
109109
AtomString module_atom = module_get_atom_string_by_id(this_module, local_module_atom_index, glb);
110110
AtomString function_atom = module_get_atom_string_by_id(this_module, local_function_atom_index, glb);
111-
uint32_t arity = READ_32_ALIGNED(table_data + i * 12 + 8 + 12);
111+
uint32_t arity = READ_32_UNALIGNED(table_data + i * 12 + 8 + 12);
112112

113113
const struct ExportedFunction *bif = bif_registry_get_handler(module_atom, function_atom, arity);
114114

@@ -140,13 +140,13 @@ static enum ModuleLoadResult module_build_imported_functions_table(Module *this_
140140
void module_get_imported_function_module_and_name(const Module *this_module, int index, AtomString *module_atom, AtomString *function_atom, GlobalContext *glb)
141141
{
142142
const uint8_t *table_data = (const uint8_t *) this_module->import_table;
143-
int functions_count = READ_32_ALIGNED(table_data + 8);
143+
int functions_count = READ_32_UNALIGNED(table_data + 8);
144144

145145
if (UNLIKELY(index > functions_count)) {
146146
AVM_ABORT();
147147
}
148-
int local_module_atom_index = READ_32_ALIGNED(table_data + index * 12 + 12);
149-
int local_function_atom_index = READ_32_ALIGNED(table_data + index * 12 + 4 + 12);
148+
int local_module_atom_index = READ_32_UNALIGNED(table_data + index * 12 + 12);
149+
int local_function_atom_index = READ_32_UNALIGNED(table_data + index * 12 + 4 + 12);
150150
*module_atom = module_get_atom_string_by_id(this_module, local_module_atom_index, glb);
151151
*function_atom = module_get_atom_string_by_id(this_module, local_function_atom_index, glb);
152152
}
@@ -156,11 +156,11 @@ bool module_get_function_from_label(Module *this_module, int label, AtomString *
156156
{
157157
int best_label = -1;
158158
const uint8_t *export_table_data = (const uint8_t *) this_module->export_table;
159-
int exports_count = READ_32_ALIGNED(export_table_data + 8);
159+
int exports_count = READ_32_UNALIGNED(export_table_data + 8);
160160
for (int export_index = exports_count - 1; export_index >= 0; export_index--) {
161-
int fun_atom_index = READ_32_ALIGNED(export_table_data + (export_index * 12) + 12);
162-
int fun_arity = READ_32_ALIGNED(export_table_data + (export_index * 12) + 4 + 12);
163-
int fun_label = READ_32_ALIGNED(export_table_data + (export_index * 12) + 8 + 12);
161+
int fun_atom_index = READ_32_UNALIGNED(export_table_data + (export_index * 12) + 12);
162+
int fun_arity = READ_32_UNALIGNED(export_table_data + (export_index * 12) + 4 + 12);
163+
int fun_label = READ_32_UNALIGNED(export_table_data + (export_index * 12) + 8 + 12);
164164
if (fun_label <= label && best_label < fun_label) {
165165
best_label = fun_label;
166166
*arity = fun_arity;
@@ -169,11 +169,11 @@ bool module_get_function_from_label(Module *this_module, int label, AtomString *
169169
}
170170

171171
const uint8_t *local_table_data = (const uint8_t *) this_module->local_table;
172-
int locals_count = READ_32_ALIGNED(local_table_data + 8);
172+
int locals_count = READ_32_UNALIGNED(local_table_data + 8);
173173
for (int local_index = locals_count - 1; local_index >= 0; local_index--) {
174-
int fun_atom_index = READ_32_ALIGNED(local_table_data + (local_index * 12) + 12);
175-
int fun_arity = READ_32_ALIGNED(local_table_data + (local_index * 12) + 4 + 12);
176-
int fun_label = READ_32_ALIGNED(local_table_data + (local_index * 12) + 8 + 12);
174+
int fun_atom_index = READ_32_UNALIGNED(local_table_data + (local_index * 12) + 12);
175+
int fun_arity = READ_32_UNALIGNED(local_table_data + (local_index * 12) + 4 + 12);
176+
int fun_label = READ_32_UNALIGNED(local_table_data + (local_index * 12) + 8 + 12);
177177
if (fun_label <= label && best_label < fun_label) {
178178
best_label = fun_label;
179179
*arity = fun_arity;
@@ -190,7 +190,7 @@ bool module_get_function_from_label(Module *this_module, int label, AtomString *
190190
size_t module_get_exported_functions_count(Module *this_module)
191191
{
192192
const uint8_t *table_data = (const uint8_t *) this_module->export_table;
193-
size_t functions_count = READ_32_ALIGNED(table_data + 8);
193+
size_t functions_count = READ_32_UNALIGNED(table_data + 8);
194194
return functions_count;
195195
}
196196

@@ -200,10 +200,10 @@ uint32_t module_search_exported_function(Module *this_module, AtomString func_na
200200

201201
const uint8_t *table_data = (const uint8_t *) this_module->export_table;
202202
for (unsigned int i = 0; i < functions_count; i++) {
203-
AtomString function_atom = module_get_atom_string_by_id(this_module, READ_32_ALIGNED(table_data + i * 12 + 12), glb);
204-
int32_t arity = READ_32_ALIGNED(table_data + i * 12 + 4 + 12);
203+
AtomString function_atom = module_get_atom_string_by_id(this_module, READ_32_UNALIGNED(table_data + i * 12 + 12), glb);
204+
int32_t arity = READ_32_UNALIGNED(table_data + i * 12 + 4 + 12);
205205
if ((func_arity == arity) && atom_are_equals(func_name, function_atom)) {
206-
uint32_t label = READ_32_ALIGNED(table_data + i * 12 + 8 + 12);
206+
uint32_t label = READ_32_UNALIGNED(table_data + i * 12 + 8 + 12);
207207
return label;
208208
}
209209
}
@@ -218,8 +218,8 @@ term module_get_exported_functions(Module *this_module, Heap *heap, GlobalContex
218218

219219
const uint8_t *table_data = (const uint8_t *) this_module->export_table;
220220
for (unsigned int i = 0; i < functions_count; i++) {
221-
AtomString function_atom = module_get_atom_string_by_id(this_module, READ_32_ALIGNED(table_data + i * 12 + 12), glb);
222-
int32_t arity = READ_32_ALIGNED(table_data + i * 12 + 4 + 12);
221+
AtomString function_atom = module_get_atom_string_by_id(this_module, READ_32_UNALIGNED(table_data + i * 12 + 12), glb);
222+
int32_t arity = READ_32_UNALIGNED(table_data + i * 12 + 4 + 12);
223223
term function_tuple = term_alloc_tuple(2, heap);
224224
term_put_tuple_element(function_tuple, 0, globalcontext_existing_term_from_atom_string(glb, function_atom));
225225
term_put_tuple_element(function_tuple, 1, term_from_int(arity));
@@ -349,7 +349,7 @@ static bool module_are_literals_compressed(const uint8_t *litT)
349349
#ifdef WITH_ZLIB
350350
static void *module_uncompress_literals(const uint8_t *litT, int size)
351351
{
352-
unsigned int required_buf_size = READ_32_ALIGNED(litT + LITT_UNCOMPRESSED_SIZE_OFFSET);
352+
unsigned int required_buf_size = READ_32_UNALIGNED(litT + LITT_UNCOMPRESSED_SIZE_OFFSET);
353353

354354
uint8_t *outBuf = malloc(required_buf_size);
355355
if (IS_NULL_PTR(outBuf)) {
@@ -384,7 +384,7 @@ static void *module_uncompress_literals(const uint8_t *litT, int size)
384384

385385
static struct LiteralEntry *module_build_literals_table(const void *literalsBuf)
386386
{
387-
uint32_t terms_count = READ_32_ALIGNED(literalsBuf);
387+
uint32_t terms_count = READ_32_UNALIGNED(literalsBuf);
388388

389389
const uint8_t *pos = (const uint8_t *) literalsBuf + sizeof(uint32_t);
390390

src/libAtomVM/module.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ static inline term module_address(unsigned int module_index, unsigned int instru
324324
static inline uint32_t module_get_fun_freeze(const Module *this_module, int fun_index)
325325
{
326326
const uint8_t *table_data = (const uint8_t *) this_module->fun_table;
327-
int funs_count = READ_32_ALIGNED(table_data + 8);
327+
int funs_count = READ_32_UNALIGNED(table_data + 8);
328328

329329
if (UNLIKELY(fun_index >= funs_count)) {
330330
AVM_ABORT();
@@ -334,7 +334,7 @@ static inline uint32_t module_get_fun_freeze(const Module *this_module, int fun_
334334
// arity
335335
// label
336336
// index
337-
uint32_t n_freeze = READ_32_ALIGNED(table_data + fun_index * 24 + 16 + 12);
337+
uint32_t n_freeze = READ_32_UNALIGNED(table_data + fun_index * 24 + 16 + 12);
338338
// ouniq
339339

340340
return n_freeze;
@@ -343,17 +343,17 @@ static inline uint32_t module_get_fun_freeze(const Module *this_module, int fun_
343343
static inline void module_get_fun(const Module *this_module, int fun_index, uint32_t *label, uint32_t *arity, uint32_t *n_freeze)
344344
{
345345
const uint8_t *table_data = (const uint8_t *) this_module->fun_table;
346-
int funs_count = READ_32_ALIGNED(table_data + 8);
346+
int funs_count = READ_32_UNALIGNED(table_data + 8);
347347

348348
if (UNLIKELY(fun_index >= funs_count)) {
349349
AVM_ABORT();
350350
}
351351

352352
// fun atom index
353-
*arity = READ_32_ALIGNED(table_data + fun_index * 24 + 4 + 12);
354-
*label = READ_32_ALIGNED(table_data + fun_index * 24 + 8 + 12);
353+
*arity = READ_32_UNALIGNED(table_data + fun_index * 24 + 4 + 12);
354+
*label = READ_32_UNALIGNED(table_data + fun_index * 24 + 8 + 12);
355355
// index
356-
*n_freeze = READ_32_ALIGNED(table_data + fun_index * 24 + 16 + 12);
356+
*n_freeze = READ_32_UNALIGNED(table_data + fun_index * 24 + 16 + 12);
357357
// ouniq
358358
}
359359

0 commit comments

Comments
 (0)