Skip to content

Commit 0299f17

Browse files
committed
Merge pull request #2266 from pguyot/w15/fix-offset-sign
Fix offset and mod_offset sign using size_t These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents bc2199c + 8264338 commit 0299f17

5 files changed

Lines changed: 30 additions & 29 deletions

File tree

src/libAtomVM/context.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,10 +1172,10 @@ COLD_FUNC void context_dump(Context *ctx)
11721172
{
11731173
Module *cp_mod;
11741174
int label;
1175-
int offset;
1175+
size_t offset;
11761176
module_cp_to_label_offset(ctx->cp, &cp_mod, &label, &offset, NULL, ctx->global);
1177-
fprintf(stderr, "cp: #CP<module: %i, label: %i, offset: %i>\n\n",
1178-
cp_mod->module_index, label, offset);
1177+
fprintf(stderr, "cp: #CP<module: %i, label: %i, offset: %u>\n\n",
1178+
cp_mod->module_index, label, (unsigned) offset);
11791179
}
11801180

11811181
fprintf(stderr, "x[0]: ");
@@ -1197,9 +1197,10 @@ COLD_FUNC void context_dump(Context *ctx)
11971197
} else if (term_is_cp(*ct)) {
11981198
Module *cp_mod;
11991199
int label;
1200-
int offset;
1200+
size_t offset;
12011201
module_cp_to_label_offset(*ct, &cp_mod, &label, &offset, NULL, ctx->global);
1202-
fprintf(stderr, "#CP<module: %i, label: %i, offset: %i>\n", cp_mod->module_index, label, offset);
1202+
// Cast offset to unsigned as some embedded libc implementations do not support %zu
1203+
fprintf(stderr, "#CP<module: %i, label: %i, offset: %u>\n", cp_mod->module_index, label, (unsigned) offset);
12031204

12041205
} else {
12051206
term_display(stderr, *ct, ctx);

src/libAtomVM/module.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,7 @@ static bool module_find_line_ref(Module *mod, uint16_t line_ref, uint32_t *line,
19141914
return module_get_location(mod, location_ix, filename_len, filename);
19151915
}
19161916

1917-
bool module_find_line(Module *mod, unsigned int offset, uint32_t *line, size_t *filename_len, const uint8_t **filename)
1917+
bool module_find_line(Module *mod, size_t offset, uint32_t *line, size_t *filename_len, const uint8_t **filename)
19181918
{
19191919
size_t i;
19201920
#ifndef AVM_NO_JIT
@@ -1980,10 +1980,10 @@ bool module_find_line(Module *mod, unsigned int offset, uint32_t *line, size_t *
19801980
#endif
19811981
}
19821982

1983-
COLD_FUNC void module_cp_to_label_offset(term cp, Module **cp_mod, int *label, int *l_off, long *out_mod_offset, GlobalContext *global)
1983+
COLD_FUNC void module_cp_to_label_offset(term cp, Module **cp_mod, int *label, size_t *l_off, size_t *out_mod_offset, GlobalContext *global)
19841984
{
19851985
Module *mod = globalcontext_get_module_by_index(global, ((uintptr_t) cp) >> 24);
1986-
long mod_offset = (cp & 0xFFFFFF) >> 2;
1986+
size_t mod_offset = (cp & 0xFFFFFF) >> 2;
19871987
if (out_mod_offset) {
19881988
*out_mod_offset = mod_offset;
19891989
}
@@ -2018,7 +2018,7 @@ COLD_FUNC void module_cp_to_label_offset(term cp, Module **cp_mod, int *label, i
20182018
*label = new_label_id;
20192019
}
20202020
if (l_off) {
2021-
*l_off = mod_offset - new_label_offset;
2021+
*l_off = 0;
20222022
}
20232023
return;
20242024
}
@@ -2042,7 +2042,7 @@ COLD_FUNC void module_cp_to_label_offset(term cp, Module **cp_mod, int *label, i
20422042

20432043
int i = 1;
20442044
const uint8_t *l = mod->labels[1];
2045-
while (mod_offset > l - code) {
2045+
while (mod_offset > (size_t) (l - code)) {
20462046
i++;
20472047
if (i >= labels_count) {
20482048
// last label + 1 is reserved for end of module.
@@ -2061,7 +2061,7 @@ COLD_FUNC void module_cp_to_label_offset(term cp, Module **cp_mod, int *label, i
20612061
*label = i - 1;
20622062
}
20632063
if (l_off) {
2064-
*l_off = mod_offset - (mod->labels[*label] - code);
2064+
*l_off = mod_offset - (mod->labels[i - 1] - code);
20652065
}
20662066
#endif
20672067
#ifndef AVM_NO_JIT

src/libAtomVM/module.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct Module
144144

145145
void *module_platform_data;
146146

147-
int end_instruction_ii;
147+
unsigned int end_instruction_ii;
148148

149149
unsigned int free_literals_data : 1;
150150

@@ -457,7 +457,7 @@ void module_insert_line_ref_offset(Module *mod, struct ListHead *line_refs, uint
457457
* @param filename on output the filename or NULL if it's module.erl. Can be NULL.
458458
* @return \c true if the line was found
459459
*/
460-
bool module_find_line(Module *mod, unsigned int offset, uint32_t *line, size_t *filename_len, const uint8_t **filename);
460+
bool module_find_line(Module *mod, size_t offset, uint32_t *line, size_t *filename_len, const uint8_t **filename);
461461

462462
/**
463463
* @return true if the module has line information, false, otherwise.
@@ -473,11 +473,11 @@ static inline bool module_has_line_chunk(Module *mod)
473473
* @param cp continuation pointer to parse
474474
* @param cp_mod if not null, set to found module
475475
* @param label if not null, set to found label
476-
* @param l_off if not null, set to offset of label from module start
476+
* @param l_off if not null, set to offset of cp from resolved label
477477
* @param mod_offset if not null, set to offset of cp from module start
478478
* @param global the global context
479479
*/
480-
void module_cp_to_label_offset(term cp, Module **cp_mod, int *label, int *l_off, long *mod_offset, GlobalContext *global);
480+
void module_cp_to_label_offset(term cp, Module **cp_mod, int *label, size_t *l_off, size_t *mod_offset, GlobalContext *global);
481481

482482
/**
483483
* @brief Get the offset of a given label from the beginning of the code, emulated or native

src/libAtomVM/stacktrace.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#ifndef AVM_CREATE_STACKTRACES
3030

31-
term stacktrace_create_raw(Context *ctx, Module *mod, int current_offset)
31+
term stacktrace_create_raw(Context *ctx, Module *mod, size_t current_offset)
3232
{
3333
UNUSED(ctx);
3434
UNUSED(mod);
@@ -37,7 +37,7 @@ term stacktrace_create_raw(Context *ctx, Module *mod, int current_offset)
3737
return context_exception_class(ctx);
3838
}
3939

40-
term stacktrace_create_raw_mfa(Context *ctx, Module *mod, int current_offset, term module_atom, term function_atom, int arity)
40+
term stacktrace_create_raw_mfa(Context *ctx, Module *mod, size_t current_offset, term module_atom, term function_atom, int arity)
4141
{
4242
UNUSED(ctx);
4343
UNUSED(mod);
@@ -102,12 +102,12 @@ static bool location_sets_append(GlobalContext *global, Module *mod, const uint8
102102
return true;
103103
}
104104

105-
term stacktrace_create_raw(Context *ctx, Module *mod, int current_offset)
105+
term stacktrace_create_raw(Context *ctx, Module *mod, size_t current_offset)
106106
{
107107
return stacktrace_create_raw_mfa(ctx, mod, current_offset, UNDEFINED_ATOM, UNDEFINED_ATOM, 0);
108108
}
109109

110-
term stacktrace_create_raw_mfa(Context *ctx, Module *mod, int current_offset, term module_atom, term function_atom, int arity)
110+
term stacktrace_create_raw_mfa(Context *ctx, Module *mod, size_t current_offset, term module_atom, term function_atom, int arity)
111111
{
112112
term exception_class = context_exception_class(ctx);
113113

@@ -150,7 +150,7 @@ term stacktrace_create_raw_mfa(Context *ctx, Module *mod, int current_offset, te
150150
unsigned int num_aux_terms = 0;
151151
size_t filename_lens = 0;
152152
Module *prev_mod = NULL;
153-
long prev_mod_offset = -1;
153+
size_t prev_mod_offset = (size_t) -1;
154154
term *ct = ctx->e;
155155
term *stack_base = context_stack_base(ctx);
156156

@@ -161,7 +161,7 @@ term stacktrace_create_raw_mfa(Context *ctx, Module *mod, int current_offset, te
161161
if (term_is_cp(*ct)) {
162162

163163
Module *cp_mod;
164-
long mod_offset;
164+
size_t mod_offset;
165165

166166
module_cp_to_label_offset(*ct, &cp_mod, NULL, NULL, &mod_offset, ctx->global);
167167
// TODO: investigate
@@ -187,7 +187,7 @@ term stacktrace_create_raw_mfa(Context *ctx, Module *mod, int current_offset, te
187187
int label = term_to_catch_label_and_module(*ct, &module_index);
188188

189189
Module *cl_mod = globalcontext_get_module_by_index(ctx->global, module_index);
190-
int mod_offset = module_label_code_offset(cl_mod, label);
190+
size_t mod_offset = module_label_code_offset(cl_mod, label);
191191

192192
if (!(prev_mod == cl_mod && mod_offset == prev_mod_offset)) {
193193
++num_frames;
@@ -214,7 +214,7 @@ term stacktrace_create_raw_mfa(Context *ctx, Module *mod, int current_offset, te
214214
uint32_t line;
215215
const uint8_t *filename;
216216
size_t filename_len;
217-
if (LIKELY(module_find_line(mod, (unsigned int) current_offset, &line, &filename_len, &filename))) {
217+
if (LIKELY(module_find_line(mod, current_offset, &line, &filename_len, &filename))) {
218218
if (!location_sets_append(ctx->global, mod, filename, filename_len, &filename_lens, &locations, &num_locations)) {
219219
return UNDEFINED_ATOM;
220220
}
@@ -289,14 +289,14 @@ term stacktrace_create_raw_mfa(Context *ctx, Module *mod, int current_offset, te
289289
raw_stacktrace = term_list_prepend(frame_info, raw_stacktrace, &ctx->heap);
290290

291291
prev_mod = NULL;
292-
prev_mod_offset = -1;
292+
prev_mod_offset = (size_t) -1;
293293
// GC may have moved stack
294294
ct = ctx->e;
295295
stack_base = context_stack_base(ctx);
296296
while (ct != stack_base) {
297297
if (term_is_cp(*ct)) {
298298
Module *cp_mod;
299-
long mod_offset;
299+
size_t mod_offset;
300300

301301
module_cp_to_label_offset(*ct, &cp_mod, NULL, NULL, &mod_offset, ctx->global);
302302
if (mod_offset != cp_mod->end_instruction_ii && !(prev_mod == cp_mod && mod_offset == prev_mod_offset)) {
@@ -315,7 +315,7 @@ term stacktrace_create_raw_mfa(Context *ctx, Module *mod, int current_offset, te
315315
int module_index;
316316
int label = term_to_catch_label_and_module(*ct, &module_index);
317317
Module *cl_mod = globalcontext_get_module_by_index(ctx->global, module_index);
318-
int mod_offset = module_label_code_offset(cl_mod, label);
318+
size_t mod_offset = module_label_code_offset(cl_mod, label);
319319

320320
if (!(prev_mod == cl_mod && mod_offset == prev_mod_offset)) {
321321

@@ -434,7 +434,7 @@ term stacktrace_build(Context *ctx, term *stack_info, uint32_t live)
434434

435435
Module *cp_mod;
436436
int label;
437-
long mod_offset;
437+
size_t mod_offset;
438438
module_cp_to_label_offset(cp, &cp_mod, &label, NULL, &mod_offset, ctx->global);
439439

440440
term module_name = module_get_name(cp_mod);

src/libAtomVM/stacktrace.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
extern "C" {
3030
#endif
3131

32-
term stacktrace_create_raw(Context *ctx, Module *mod, int current_offset);
32+
term stacktrace_create_raw(Context *ctx, Module *mod, size_t current_offset);
3333

34-
term stacktrace_create_raw_mfa(Context *ctx, Module *mod, int current_offset, term module_atom,
34+
term stacktrace_create_raw_mfa(Context *ctx, Module *mod, size_t current_offset, term module_atom,
3535
term function_atom, int arity);
3636

3737
/**

0 commit comments

Comments
 (0)