Skip to content

Commit 9bc311d

Browse files
committed
Forward port changes from v0.6 release branch
Merge MBEDTLS_ECP_FIXED_POINT_OPTIM default change (atomvm#1558) and lage file line numbers (atomvm#1557) from release-0.6 branch.
2 parents bc73bf9 + f556340 commit 9bc311d

File tree

9 files changed

+2113
-62
lines changed

9 files changed

+2113
-62
lines changed

src/libAtomVM/module.c

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@
4343
#define LITT_UNCOMPRESSED_SIZE_OFFSET 8
4444
#define LITT_HEADER_SIZE 12
4545

46-
// TODO Constants similar to these are defined in opcodesswitch.h and should
47-
// be refactored so they can be used here, as well.
48-
#define TAG_COMPACT_INT 0x01
49-
#define TAG_COMPACT_ATOM 0x02
50-
#define TAG_EXTENDED_INT 0x09
51-
#define TAG_EXTENDED_ATOM 0x0A
52-
5346
#define CHECK_FREE_SPACE(space, error) \
5447
if ((size_t) ((pos + space) - data) > len) { \
5548
fprintf(stderr, error); \
@@ -471,22 +464,39 @@ static bool module_check_line_refs(Module *mod, const uint8_t **data, size_t len
471464
}
472465
uint8_t tag = *pos;
473466
switch (tag & 0x0F) {
474-
case TAG_COMPACT_INT: {
467+
case COMPACT_INTEGER: {
475468
++i;
476469
++pos;
477470
break;
478471
}
479-
case TAG_EXTENDED_INT: {
472+
case COMPACT_LARGE_INTEGER: {
480473
++pos;
474+
switch (tag & COMPACT_LARGE_IMM_MASK) {
475+
case COMPACT_11BITS_VALUE: {
476+
++pos;
477+
break;
478+
}
479+
case COMPACT_NBITS_VALUE: {
480+
int sz = (tag >> 5) + 2;
481+
if (UNLIKELY(sz > 4)) {
482+
fprintf(stderr, "Invalid line_ref: expected extended int with sz <= 4 (line number <= 2^31)");
483+
return false;
484+
}
485+
pos += sz;
486+
break;
487+
}
488+
default:
489+
fprintf(stderr, "Invalid line_ref: expected extended int -- tag = %u", (unsigned int) tag);
490+
return false;
491+
}
481492
if ((size_t) (pos - *data) > len) {
482493
fprintf(stderr, "Invalid line_ref: expected extended int.\n");
483494
return false;
484495
}
485496
++i;
486-
++pos;
487497
break;
488498
}
489-
case TAG_COMPACT_ATOM: {
499+
case COMPACT_ATOM: {
490500
uint16_t location_ix = ((tag & 0xF0) >> 4);
491501
if (location_ix > mod->locations_count) {
492502
fprintf(stderr, "Invalid line_ref: location_ix = %d is greater than locations_count = %d.\n", (int) location_ix, (int) mod->locations_count);
@@ -495,11 +505,16 @@ static bool module_check_line_refs(Module *mod, const uint8_t **data, size_t len
495505
++pos;
496506
break;
497507
}
498-
case TAG_EXTENDED_ATOM: {
508+
case COMPACT_LARGE_ATOM: {
509+
// We don't support more than 11bits (2048) locations.
510+
if (UNLIKELY((tag & COMPACT_LARGE_IMM_MASK) != COMPACT_11BITS_VALUE)) {
511+
fprintf(stderr, "Invalid line_ref: location_ix is larger than 2048.\n");
512+
return false;
513+
}
499514
uint16_t high_order_3_bits = (tag & 0xE0);
500515
++pos;
501516
if ((size_t) (pos - *data) > len) {
502-
fprintf(stderr, "Invalid line_ref: expected extended int.\n");
517+
fprintf(stderr, "Invalid line_ref: expected extended atom.\n");
503518
return false;
504519
}
505520
uint8_t next_byte = *pos;
@@ -512,7 +527,6 @@ static bool module_check_line_refs(Module *mod, const uint8_t **data, size_t len
512527
break;
513528
}
514529
default:
515-
// TODO handle integer compact encodings > 2048
516530
fprintf(stderr, "Unsupported line_ref tag: %u\n", tag);
517531
return false;
518532
}
@@ -542,7 +556,7 @@ static bool module_check_locations(Module *mod, const uint8_t *data, size_t len)
542556
return true;
543557
}
544558

545-
static bool module_get_line_ref(Module *mod, uint16_t line_ref, uint16_t *out_line, uint16_t *out_location)
559+
static bool module_get_line_ref(Module *mod, uint16_t line_ref, uint32_t *out_line, uint16_t *out_location)
546560
{
547561
// First is undefined
548562
if (line_ref == 0) {
@@ -557,9 +571,9 @@ static bool module_get_line_ref(Module *mod, uint16_t line_ref, uint16_t *out_li
557571
while (i <= mod->line_refs_count) {
558572
uint8_t tag = *pos;
559573
switch (tag & 0x0F) {
560-
case TAG_COMPACT_INT: {
574+
case COMPACT_INTEGER: {
561575
if (i == line_ref) {
562-
uint16_t line_idx = ((tag & 0xF0) >> 4);
576+
uint32_t line_idx = ((tag & 0xF0) >> 4);
563577
*out_line = line_idx;
564578
*out_location = location_ix;
565579
return true;
@@ -568,32 +582,49 @@ static bool module_get_line_ref(Module *mod, uint16_t line_ref, uint16_t *out_li
568582
++pos;
569583
break;
570584
}
571-
case TAG_EXTENDED_INT: {
585+
case COMPACT_LARGE_INTEGER: {
586+
uint32_t line_idx;
587+
switch (tag & COMPACT_LARGE_IMM_MASK) {
588+
case COMPACT_11BITS_VALUE: {
589+
uint16_t high_order_3_bits = (tag & 0xE0);
590+
line_idx = ((high_order_3_bits << 3) | pos[1]);
591+
pos += 2;
592+
break;
593+
}
594+
case COMPACT_NBITS_VALUE: {
595+
pos++;
596+
int sz = (tag >> 5) + 2;
597+
line_idx = 0;
598+
for (int i = 0; i < sz; i++) {
599+
line_idx = line_idx * 256 + pos[i];
600+
}
601+
pos += sz;
602+
break;
603+
}
604+
default:
605+
UNREACHABLE();
606+
}
572607
if (i == line_ref) {
573-
uint16_t high_order_3_bits = (tag & 0xE0);
574-
uint16_t line_idx = ((high_order_3_bits << 3) | pos[1]);
575608
*out_line = line_idx;
576609
*out_location = location_ix;
577610
return true;
578611
}
579-
pos += 2;
580612
++i;
581613
break;
582614
}
583-
case TAG_COMPACT_ATOM: {
615+
case COMPACT_ATOM: {
584616
location_ix = ((tag & 0xF0) >> 4);
585617
++pos;
586618
break;
587619
}
588-
case TAG_EXTENDED_ATOM: {
620+
case COMPACT_LARGE_ATOM: {
589621
uint16_t high_order_3_bits = (tag & 0xE0);
590622
location_ix = ((high_order_3_bits << 3) | pos[1]);
591623
pos += 2;
592624
break;
593625
}
594626
default:
595-
// TODO handle integer compact encodings > 2048
596-
return false;
627+
UNREACHABLE();
597628
}
598629
}
599630

@@ -702,7 +733,7 @@ void module_insert_line_ref_offset(Module *mod, int line_ref, int offset)
702733
list_append(&mod->line_ref_offsets, &ref_offset->head);
703734
}
704735

705-
static bool module_find_line_ref(Module *mod, uint16_t line_ref, uint16_t *line, size_t *filename_len, const uint8_t **filename)
736+
static bool module_find_line_ref(Module *mod, uint16_t line_ref, uint32_t *line, size_t *filename_len, const uint8_t **filename)
706737
{
707738
uint16_t location_ix;
708739
if (UNLIKELY(!module_get_line_ref(mod, line_ref, line, &location_ix))) {
@@ -711,7 +742,7 @@ static bool module_find_line_ref(Module *mod, uint16_t line_ref, uint16_t *line,
711742
return module_get_location(mod, location_ix, filename_len, filename);
712743
}
713744

714-
bool module_find_line(Module *mod, unsigned int offset, uint16_t *line, size_t *filename_len, const uint8_t **filename)
745+
bool module_find_line(Module *mod, unsigned int offset, uint32_t *line, size_t *filename_len, const uint8_t **filename)
715746
{
716747
int i = 0;
717748
struct LineRefOffset *head = GET_LIST_ENTRY(&mod->line_ref_offsets, struct LineRefOffset, head);

src/libAtomVM/module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ void module_insert_line_ref_offset(Module *mod, int line_ref, int offset);
411411
* @param filename on output the filename or NULL if it's module.erl. Can be NULL.
412412
* @return \c true if the line was found
413413
*/
414-
bool module_find_line(Module *mod, unsigned int offset, uint16_t *line, size_t *filename_len, const uint8_t **filename);
414+
bool module_find_line(Module *mod, unsigned int offset, uint32_t *line, size_t *filename_len, const uint8_t **filename);
415415

416416
/**
417417
* @return true if the module has line information, false, otherwise.

src/libAtomVM/stacktrace.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ term stacktrace_create_raw(Context *ctx, Module *mod, int current_offset, term e
139139
prev_mod = cp_mod;
140140
prev_mod_offset = mod_offset;
141141
if (module_has_line_chunk(cp_mod)) {
142-
uint16_t line;
142+
uint32_t line;
143143
const uint8_t *filename;
144144
size_t filename_len;
145145
if (LIKELY(module_find_line(cp_mod, (unsigned int) mod_offset, &line, &filename_len, &filename))) {
@@ -163,7 +163,7 @@ term stacktrace_create_raw(Context *ctx, Module *mod, int current_offset, term e
163163
prev_mod = cl_mod;
164164
prev_mod_offset = mod_offset;
165165
if (module_has_line_chunk(cl_mod)) {
166-
uint16_t line;
166+
uint32_t line;
167167
const uint8_t *filename;
168168
size_t filename_len;
169169
if (LIKELY(module_find_line(cl_mod, (unsigned int) mod_offset, &line, &filename_len, &filename))) {
@@ -180,7 +180,7 @@ term stacktrace_create_raw(Context *ctx, Module *mod, int current_offset, term e
180180

181181
num_frames++;
182182
if (module_has_line_chunk(mod)) {
183-
uint16_t line;
183+
uint32_t line;
184184
const uint8_t *filename;
185185
size_t filename_len;
186186
if (LIKELY(module_find_line(mod, (unsigned int) current_offset, &line, &filename_len, &filename))) {
@@ -342,7 +342,7 @@ term stacktrace_build(Context *ctx, term *stack_info, uint32_t live)
342342

343343
term aux_data = term_nil();
344344
if (module_has_line_chunk(cp_mod)) {
345-
uint16_t line;
345+
uint32_t line;
346346
const uint8_t *filename;
347347
size_t filename_len;
348348
if (LIKELY(module_find_line(cp_mod, (unsigned int) mod_offset, &line, &filename_len, &filename))) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
CONFIG_PARTITION_TABLE_CUSTOM=y
22
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
3+
CONFIG_MBEDTLS_ECP_FIXED_POINT_OPTIM=y
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CONFIG_PARTITION_TABLE_CUSTOM=y
22
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
3+
CONFIG_MBEDTLS_ECP_FIXED_POINT_OPTIM=y
34
CONFIG_COMPILER_OPTIMIZATION_PERF=y
45
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y

src/platforms/esp32/test/sdkconfig.ci.wokwi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ CONFIG_AVM_RTC_SLOW_MAX_SIZE=1024
55
CONFIG_ETH_USE_OPENETH=n
66
CONFIG_COMPILER_OPTIMIZATION_PERF=y
77
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
8+
CONFIG_MBEDTLS_ECP_FIXED_POINT_OPTIM=y

src/platforms/esp32/test/sdkconfig.defaults

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=n
44
CONFIG_ESP_INT_WDT_TIMEOUT_MS=10000
55
CONFIG_ETH_USE_OPENETH=y
66
CONFIG_AVM_RTC_SLOW_MAX_SIZE=1024
7+
CONFIG_MBEDTLS_ECP_FIXED_POINT_OPTIM=y

tests/erlang_tests/test_stacktrace.erl

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
-module(test_stacktrace).
2121

2222
-export([start/0, maybe_crash/1]).
23+
-export([
24+
throw_with_file_and_line/0,
25+
throw_with_other_file_and_line/0,
26+
throw_with_other_file_and_line_large_value/0
27+
]).
2328

2429
-include("test_stacktrace.hrl").
2530

@@ -36,6 +41,7 @@ start() ->
3641
ok = test_catch(),
3742
ok = maybe_test_filelineno(),
3843
ok = maybe_test_filelineno_other_file(),
44+
ok = maybe_test_filelineno_large(),
3945
0.
4046

4147
test_local_throw() ->
@@ -233,15 +239,15 @@ test_catch() ->
233239
do_some_stuff(Result),
234240
Result.
235241

236-
maybe_test_filelineno() ->
242+
maybe_test_filelineno(Fun) ->
237243
ok =
238244
try
239-
throw_with_file_and_line(),
245+
?MODULE:Fun(),
240246
fail
241247
catch
242248
throw:{File, Line}:Stacktrace ->
243249
[Frame | _] = Stacktrace,
244-
{?MODULE, throw_with_file_and_line, 0, AuxData} = Frame,
250+
{?MODULE, Fun, 0, AuxData} = Frame,
245251
case {get_value(file, AuxData), get_value(line, AuxData)} of
246252
{undefined, undefined} ->
247253
ok;
@@ -262,34 +268,16 @@ maybe_test_filelineno() ->
262268
end
263269
end.
264270

271+
maybe_test_filelineno() ->
272+
maybe_test_filelineno(throw_with_file_and_line).
273+
265274
maybe_test_filelineno_other_file() ->
266-
ok =
267-
try
268-
throw_with_other_file_and_line(),
269-
fail
270-
catch
271-
throw:{File, Line}:Stacktrace ->
272-
[Frame | _] = Stacktrace,
273-
{?MODULE, throw_with_other_file_and_line, 0, AuxData} = Frame,
274-
case {get_value(file, AuxData), get_value(line, AuxData)} of
275-
{undefined, undefined} ->
276-
ok;
277-
{F, L} ->
278-
Ef =
279-
case is_binary(F) of
280-
true ->
281-
erlang:binary_to_list(F);
282-
_ ->
283-
F
284-
end,
285-
case File == Ef andalso Line == L of
286-
true ->
287-
ok;
288-
_ ->
289-
{unexpected_file_line, F, L}
290-
end
291-
end
292-
end.
275+
maybe_test_filelineno(throw_with_other_file_and_line).
276+
277+
% This test actually succeeds even if large line numbers are not supported
278+
% because all line numbers are then disabled.
279+
maybe_test_filelineno_large() ->
280+
maybe_test_filelineno(throw_with_other_file_and_line_large_value).
293281

294282
get_value(_Key, []) ->
295283
undefined;

0 commit comments

Comments
 (0)