Skip to content

Commit d8d878a

Browse files
committed
JIT: box integers when parsing types
Fix crash on armv6m when decoding types as term_from_int64 would fail Signed-off-by: Paul Guyot <[email protected]>
1 parent 572d232 commit d8d878a

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/libAtomVM/module.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -637,19 +637,26 @@ term module_get_type_by_index(const Module *mod, int type_index, Context *ctx)
637637
case (BEAM_TYPE_FLOAT | BEAM_TYPE_INTEGER):
638638
// {t_number, {LowerBound, UpperBound}}
639639
if (has_lower || has_upper) {
640-
if (UNLIKELY(memory_ensure_free(ctx, TUPLE_SIZE(2) + TUPLE_SIZE(2)) != MEMORY_GC_OK)) {
640+
size_t heap_size = TUPLE_SIZE(2) + TUPLE_SIZE(2);
641+
if (has_lower && (lower_bound < MIN_NOT_BOXED_INT || lower_bound > MAX_NOT_BOXED_INT)) {
642+
heap_size += BOXED_INT64_SIZE;
643+
}
644+
if (has_upper && (upper_bound < MIN_NOT_BOXED_INT || upper_bound > MAX_NOT_BOXED_INT)) {
645+
heap_size += BOXED_INT64_SIZE;
646+
}
647+
if (UNLIKELY(memory_ensure_free(ctx, heap_size) != MEMORY_GC_OK)) {
641648
return globalcontext_make_atom(ctx->global, ATOM_STR("\x3", "any"));
642649
}
643650
term bounds_tuple = term_alloc_tuple(2, &ctx->heap);
644651

645652
if (has_lower) {
646-
term_put_tuple_element(bounds_tuple, 0, term_from_int64(lower_bound));
653+
term_put_tuple_element(bounds_tuple, 0, term_make_maybe_boxed_int64(lower_bound, &ctx->heap));
647654
} else {
648655
term_put_tuple_element(bounds_tuple, 0, globalcontext_make_atom(ctx->global, ATOM_STR("\x4", "-inf")));
649656
}
650657

651658
if (has_upper) {
652-
term_put_tuple_element(bounds_tuple, 1, term_from_int64(upper_bound));
659+
term_put_tuple_element(bounds_tuple, 1, term_make_maybe_boxed_int64(upper_bound, &ctx->heap));
653660
} else {
654661
term_put_tuple_element(bounds_tuple, 1, globalcontext_make_atom(ctx->global, ATOM_STR("\x4", "+inf")));
655662
}
@@ -663,19 +670,26 @@ term module_get_type_by_index(const Module *mod, int type_index, Context *ctx)
663670

664671
case BEAM_TYPE_INTEGER:
665672
if (has_lower || has_upper) {
666-
if (UNLIKELY(memory_ensure_free(ctx, TUPLE_SIZE(2) + TUPLE_SIZE(2)) != MEMORY_GC_OK)) {
673+
size_t heap_size = TUPLE_SIZE(2) + TUPLE_SIZE(2);
674+
if (has_lower && (lower_bound < MIN_NOT_BOXED_INT || lower_bound > MAX_NOT_BOXED_INT)) {
675+
heap_size += BOXED_INT64_SIZE;
676+
}
677+
if (has_upper && (upper_bound < MIN_NOT_BOXED_INT || upper_bound > MAX_NOT_BOXED_INT)) {
678+
heap_size += BOXED_INT64_SIZE;
679+
}
680+
if (UNLIKELY(memory_ensure_free(ctx, heap_size) != MEMORY_GC_OK)) {
667681
return globalcontext_make_atom(ctx->global, ATOM_STR("\x3", "any"));
668682
}
669683
term bounds_tuple = term_alloc_tuple(2, &ctx->heap);
670684

671685
if (has_lower) {
672-
term_put_tuple_element(bounds_tuple, 0, term_from_int64(lower_bound));
686+
term_put_tuple_element(bounds_tuple, 0, term_make_maybe_boxed_int64(lower_bound, &ctx->heap));
673687
} else {
674688
term_put_tuple_element(bounds_tuple, 0, globalcontext_make_atom(ctx->global, ATOM_STR("\x4", "-inf")));
675689
}
676690

677691
if (has_upper) {
678-
term_put_tuple_element(bounds_tuple, 1, term_from_int64(upper_bound));
692+
term_put_tuple_element(bounds_tuple, 1, term_make_maybe_boxed_int64(upper_bound, &ctx->heap));
679693
} else {
680694
term_put_tuple_element(bounds_tuple, 1, globalcontext_make_atom(ctx->global, ATOM_STR("\x4", "+inf")));
681695
}

0 commit comments

Comments
 (0)