Skip to content

Commit d9c586c

Browse files
committed
Deprecate term_from_int32 and term_from_int64
Introduce `term_from_int28` that is always safe and update usage in main library and generic_unix port Signed-off-by: Paul Guyot <[email protected]>
1 parent d8d878a commit d9c586c

File tree

8 files changed

+62
-46
lines changed

8 files changed

+62
-46
lines changed

src/libAtomVM/bif.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ term bif_erlang_byte_size_1(Context *ctx, uint32_t fail_label, int live, term ar
9494
len = term_binary_size(arg1);
9595
}
9696

97-
return term_from_int32(len);
97+
return term_from_int(len);
9898
}
9999

100100
term bif_erlang_bit_size_1(Context *ctx, uint32_t fail_label, int live, term arg1)
@@ -112,7 +112,7 @@ term bif_erlang_bit_size_1(Context *ctx, uint32_t fail_label, int live, term arg
112112
len = term_binary_size(arg1) * 8;
113113
}
114114

115-
return term_from_int32(len);
115+
return term_from_int(len);
116116
}
117117

118118
term bif_erlang_binary_part_3(Context *ctx, uint32_t fail_label, int live, term arg1, term arg2, term arg3)
@@ -373,7 +373,8 @@ term bif_erlang_tuple_size_1(Context *ctx, uint32_t fail_label, term arg1)
373373
{
374374
VALIDATE_VALUE_BIF(fail_label, arg1, term_is_tuple);
375375

376-
return term_from_int32(term_get_tuple_arity(arg1));
376+
// tuple size is (term) header >> 6, so we know it fits a term as an int
377+
return term_from_int(term_get_tuple_arity(arg1));
377378
}
378379

379380
term bif_erlang_map_size_1(Context *ctx, uint32_t fail_label, int live, term arg1)
@@ -395,7 +396,8 @@ term bif_erlang_map_size_1(Context *ctx, uint32_t fail_label, int live, term arg
395396
RAISE_ERROR(err);
396397
}
397398

398-
return term_from_int32(term_get_map_size(arg1));
399+
// map size is (term) header >> 6, so we know it fits a term as an int
400+
return term_from_int(term_get_map_size(arg1));
399401
}
400402

401403
term bif_erlang_map_get_2(Context *ctx, uint32_t fail_label, term arg1, term arg2)

src/libAtomVM/context.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ bool context_get_process_info(Context *ctx, term *out, size_t *term_size, term a
579579
case HEAP_SIZE_ATOM: {
580580
term_put_tuple_element(ret, 0, HEAP_SIZE_ATOM);
581581
unsigned long value = memory_heap_youngest_size(&ctx->heap);
582-
term_put_tuple_element(ret, 1, term_from_int32(value));
582+
term_put_tuple_element(ret, 1, term_from_int(value));
583583
break;
584584
}
585585

@@ -599,31 +599,31 @@ bool context_get_process_info(Context *ctx, term *out, size_t *term_size, term a
599599
case TOTAL_HEAP_SIZE_ATOM: {
600600
term_put_tuple_element(ret, 0, TOTAL_HEAP_SIZE_ATOM);
601601
unsigned long value = memory_heap_memory_size(&ctx->heap);
602-
term_put_tuple_element(ret, 1, term_from_int32(value));
602+
term_put_tuple_element(ret, 1, term_from_int(value));
603603
break;
604604
}
605605

606606
// stack_size stack size, in words, of the process
607607
case STACK_SIZE_ATOM: {
608608
term_put_tuple_element(ret, 0, STACK_SIZE_ATOM);
609609
unsigned long value = context_stack_size(ctx);
610-
term_put_tuple_element(ret, 1, term_from_int32(value));
610+
term_put_tuple_element(ret, 1, term_from_int(value));
611611
break;
612612
}
613613

614614
// message_queue_len number of messages currently in the message queue of the process
615615
case MESSAGE_QUEUE_LEN_ATOM: {
616616
term_put_tuple_element(ret, 0, MESSAGE_QUEUE_LEN_ATOM);
617617
unsigned long value = context_message_queue_len(ctx);
618-
term_put_tuple_element(ret, 1, term_from_int32(value));
618+
term_put_tuple_element(ret, 1, term_from_int(value));
619619
break;
620620
}
621621

622622
// memory size in bytes of the process. This includes call stack, heap, and internal structures.
623623
case MEMORY_ATOM: {
624624
term_put_tuple_element(ret, 0, MEMORY_ATOM);
625625
unsigned long value = context_size(ctx);
626-
term_put_tuple_element(ret, 1, term_from_int32(value));
626+
term_put_tuple_element(ret, 1, term_from_int(value));
627627
break;
628628
}
629629

src/libAtomVM/inet.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ uint32_t inet_addr4_to_uint32(term addr_tuple)
7272
term inet_make_addr4(uint32_t addr, Heap *heap)
7373
{
7474
term result = term_alloc_tuple(4, heap);
75-
term_put_tuple_element(result, 0, term_from_int32((addr >> 24) & 0xFF));
76-
term_put_tuple_element(result, 1, term_from_int32((addr >> 16) & 0xFF));
77-
term_put_tuple_element(result, 2, term_from_int32((addr >> 8) & 0xFF));
78-
term_put_tuple_element(result, 3, term_from_int32(addr & 0xFF));
75+
term_put_tuple_element(result, 0, term_from_int11((addr >> 24) & 0xFF));
76+
term_put_tuple_element(result, 1, term_from_int11((addr >> 16) & 0xFF));
77+
term_put_tuple_element(result, 2, term_from_int11((addr >> 8) & 0xFF));
78+
term_put_tuple_element(result, 3, term_from_int11(addr & 0xFF));
7979
return result;
8080
}

src/libAtomVM/module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ term module_get_type_by_index(const Module *mod, int type_index, Context *ctx)
622622
}
623623
term type_tuple = term_alloc_tuple(2, &ctx->heap);
624624
term_put_tuple_element(type_tuple, 0, globalcontext_make_atom(ctx->global, ATOM_STR("\xE", "t_bs_matchable")));
625-
term_put_tuple_element(type_tuple, 1, term_from_int32(unit));
625+
term_put_tuple_element(type_tuple, 1, term_from_int11(unit));
626626
return type_tuple;
627627

628628
case BEAM_TYPE_CONS:

src/libAtomVM/nifs.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,13 +1687,13 @@ static term build_datetime_from_tm(Context *ctx, struct tm *broken_down_time)
16871687
term time_tuple = term_alloc_tuple(3, &ctx->heap);
16881688
term date_time_tuple = term_alloc_tuple(2, &ctx->heap);
16891689

1690-
term_put_tuple_element(date_tuple, 0, term_from_int32(1900 + broken_down_time->tm_year));
1691-
term_put_tuple_element(date_tuple, 1, term_from_int32(broken_down_time->tm_mon + 1));
1692-
term_put_tuple_element(date_tuple, 2, term_from_int32(broken_down_time->tm_mday));
1690+
term_put_tuple_element(date_tuple, 0, term_from_int11(1900 + broken_down_time->tm_year));
1691+
term_put_tuple_element(date_tuple, 1, term_from_int11(broken_down_time->tm_mon + 1));
1692+
term_put_tuple_element(date_tuple, 2, term_from_int11(broken_down_time->tm_mday));
16931693

1694-
term_put_tuple_element(time_tuple, 0, term_from_int32(broken_down_time->tm_hour));
1695-
term_put_tuple_element(time_tuple, 1, term_from_int32(broken_down_time->tm_min));
1696-
term_put_tuple_element(time_tuple, 2, term_from_int32(broken_down_time->tm_sec));
1694+
term_put_tuple_element(time_tuple, 0, term_from_int11(broken_down_time->tm_hour));
1695+
term_put_tuple_element(time_tuple, 1, term_from_int11(broken_down_time->tm_min));
1696+
term_put_tuple_element(time_tuple, 2, term_from_int11(broken_down_time->tm_sec));
16971697

16981698
term_put_tuple_element(date_time_tuple, 0, date_tuple);
16991699
term_put_tuple_element(date_time_tuple, 1, time_tuple);
@@ -1772,9 +1772,9 @@ term nif_erlang_timestamp_0(Context *ctx, int argc, term argv[])
17721772
struct timespec ts;
17731773
sys_time(&ts);
17741774

1775-
term_put_tuple_element(timestamp_tuple, 0, term_from_int32(ts.tv_sec / 1000000));
1776-
term_put_tuple_element(timestamp_tuple, 1, term_from_int32(ts.tv_sec % 1000000));
1777-
term_put_tuple_element(timestamp_tuple, 2, term_from_int32(ts.tv_nsec / 1000));
1775+
term_put_tuple_element(timestamp_tuple, 0, term_from_int28(ts.tv_sec / 1000000));
1776+
term_put_tuple_element(timestamp_tuple, 1, term_from_int28(ts.tv_sec % 1000000));
1777+
term_put_tuple_element(timestamp_tuple, 2, term_from_int28(ts.tv_nsec / 1000));
17781778

17791779
return timestamp_tuple;
17801780
}
@@ -2943,16 +2943,16 @@ static term nif_erlang_system_info(Context *ctx, int argc, term argv[])
29432943
}
29442944

29452945
if (key == PROCESS_COUNT_ATOM) {
2946-
return term_from_int32(nif_num_processes(ctx->global));
2946+
return term_from_int28(nif_num_processes(ctx->global));
29472947
}
29482948
if (key == PORT_COUNT_ATOM) {
2949-
return term_from_int32(nif_num_ports(ctx->global));
2949+
return term_from_int28(nif_num_ports(ctx->global));
29502950
}
29512951
if (key == ATOM_COUNT_ATOM) {
2952-
return term_from_int32(atom_table_count(ctx->global->atom_table));
2952+
return term_from_int28(atom_table_count(ctx->global->atom_table));
29532953
}
29542954
if (key == WORDSIZE_ATOM) {
2955-
return term_from_int32(TERM_BYTES);
2955+
return term_from_int28(TERM_BYTES);
29562956
}
29572957
if (key == MACHINE_ATOM) {
29582958
if (memory_ensure_free_opt(ctx, (sizeof("ATOM") - 1) * 2, MEMORY_CAN_SHRINK) != MEMORY_GC_OK) {
@@ -2961,7 +2961,7 @@ static term nif_erlang_system_info(Context *ctx, int argc, term argv[])
29612961
return term_from_string((const uint8_t *) "ATOM", sizeof("ATOM") - 1, &ctx->heap);
29622962
}
29632963
if (key == AVM_FLOATSIZE_ATOM) {
2964-
return term_from_int32(sizeof(avm_float_t));
2964+
return term_from_int11(sizeof(avm_float_t));
29652965
}
29662966
if (key == SYSTEM_ARCHITECTURE_ATOM) {
29672967
char buf[128];
@@ -3008,16 +3008,16 @@ static term nif_erlang_system_info(Context *ctx, int argc, term argv[])
30083008
}
30093009
if (key == SCHEDULERS_ATOM) {
30103010
#ifndef AVM_NO_SMP
3011-
return term_from_int32(smp_get_online_processors());
3011+
return term_from_int11(smp_get_online_processors());
30123012
#else
3013-
return term_from_int32(1);
3013+
return term_from_int11(1);
30143014
#endif
30153015
}
30163016
if (key == SCHEDULERS_ONLINE_ATOM) {
30173017
#ifndef AVM_NO_SMP
3018-
return term_from_int32(ctx->global->online_schedulers);
3018+
return term_from_int11(ctx->global->online_schedulers);
30193019
#else
3020-
return term_from_int32(1);
3020+
return term_from_int11(1);
30213021
#endif
30223022
}
30233023
if (key == EMU_FLAVOR_ATOM) {
@@ -3055,7 +3055,7 @@ static term nif_erlang_system_flag(Context *ctx, int argc, term argv[])
30553055
}
30563056
while (!ATOMIC_COMPARE_EXCHANGE_WEAK_INT(&ctx->global->online_schedulers, &old_value, new_value)) {
30573057
};
3058-
return term_from_int32(old_value);
3058+
return term_from_int11(old_value);
30593059
}
30603060
#else
30613061
UNUSED(value);
@@ -3802,7 +3802,7 @@ static term nif_erts_debug_flat_size(Context *ctx, int argc, term argv[])
38023802

38033803
terms_count = memory_estimate_usage(argv[0]);
38043804

3805-
return term_from_int32(terms_count);
3805+
return term_from_int28(terms_count);
38063806
}
38073807

38083808
static term make_list_from_ascii_buf(const uint8_t *buf, size_t len, Context *ctx)

src/libAtomVM/port.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ term port_heap_create_error_tuple(Heap *heap, term reason)
9999

100100
term port_heap_create_sys_error_tuple(Heap *heap, term syscall, int errno)
101101
{
102-
term reason = port_heap_create_tuple2(heap, syscall, term_from_int32(errno));
102+
term reason = port_heap_create_tuple2(heap, syscall, term_from_int(errno));
103103
return port_heap_create_error_tuple(heap, reason);
104104
}
105105

src/libAtomVM/term.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,13 +947,26 @@ static inline term term_from_int11(int16_t value)
947947
return (value << 4) | TERM_INTEGER_TAG;
948948
}
949949

950+
/**
951+
* @brief Term from int28
952+
*
953+
* @details Returns a term for a given 28 bits integer value.
954+
* @param value the value that will be converted to a term.
955+
* @return a term that encapsulates the integer value.
956+
*/
957+
static inline term term_from_int28(int32_t value)
958+
{
959+
return (value << 4) | TERM_INTEGER_TAG;
960+
}
961+
950962
/**
951963
* @brief Term from int32
952964
*
953965
* @details Returns a term for a given 32 bits integer value.
954966
* @param value the value that will be converted to a term.
955967
* @return a term that encapsulates the integer value.
956968
*/
969+
static inline term term_from_int32(int32_t value) __attribute__ ((deprecated ("term_from_int32 is unsafe and will be removed")));
957970
static inline term term_from_int32(int32_t value)
958971
{
959972
#if TERM_BITS == 32
@@ -975,6 +988,7 @@ static inline term term_from_int32(int32_t value)
975988
#endif
976989
}
977990

991+
static inline term term_from_int64(int64_t value) __attribute__ ((deprecated ("term_from_int64 is unsafe and will be removed")));
978992
static inline term term_from_int64(int64_t value)
979993
{
980994
#if TERM_BITS == 32

src/platforms/generic_unix/lib/socket_driver.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,21 @@ uint32_t socket_tuple_to_addr(term addr_tuple)
143143
term socket_ctx_tuple_from_addr(Context *ctx, uint32_t addr)
144144
{
145145
term terms[4];
146-
terms[0] = term_from_int32((addr >> 24) & 0xFF);
147-
terms[1] = term_from_int32((addr >> 16) & 0xFF);
148-
terms[2] = term_from_int32((addr >> 8) & 0xFF);
149-
terms[3] = term_from_int32(addr & 0xFF);
146+
terms[0] = term_from_int11((addr >> 24) & 0xFF);
147+
terms[1] = term_from_int11((addr >> 16) & 0xFF);
148+
terms[2] = term_from_int11((addr >> 8) & 0xFF);
149+
terms[3] = term_from_int11(addr & 0xFF);
150150

151151
return port_create_tuple_n(ctx, 4, terms);
152152
}
153153

154154
term socket_heap_tuple_from_addr(Heap *heap, uint32_t addr)
155155
{
156156
term terms[4];
157-
terms[0] = term_from_int32((addr >> 24) & 0xFF);
158-
terms[1] = term_from_int32((addr >> 16) & 0xFF);
159-
terms[2] = term_from_int32((addr >> 8) & 0xFF);
160-
terms[3] = term_from_int32(addr & 0xFF);
157+
terms[0] = term_from_int11((addr >> 24) & 0xFF);
158+
terms[1] = term_from_int11((addr >> 16) & 0xFF);
159+
terms[2] = term_from_int11((addr >> 8) & 0xFF);
160+
terms[3] = term_from_int11(addr & 0xFF);
161161

162162
return port_heap_create_tuple_n(heap, 4, terms);
163163
}
@@ -686,7 +686,7 @@ term socket_driver_do_sendto(Context *ctx, term dest_address, term dest_port, te
686686
return port_create_sys_error_tuple(ctx, SENDTO_ATOM, errno);
687687
} else {
688688
TRACE("socket_driver_do_sendto: sent data with len: %li, to: %i, port: %i\n", len, ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port));
689-
term sent_atom = term_from_int32(sent_data);
689+
term sent_atom = term_from_int(sent_data);
690690
return port_create_ok_tuple(ctx, sent_atom);
691691
}
692692
}
@@ -892,7 +892,7 @@ static EventListener *active_recvfrom_callback(GlobalContext *glb, EventListener
892892
}
893893
term pid = socket_data->controlling_process;
894894
term addr = socket_heap_tuple_from_addr(&heap, htonl(clientaddr.sin_addr.s_addr));
895-
term port = term_from_int32(htons(clientaddr.sin_port));
895+
term port = term_from_int28(htons(clientaddr.sin_port));
896896
term packet = socket_create_packet_term(buf, len, socket_data->binary, &heap, glb);
897897
term socket_pid = term_port_from_local_process_id(ctx->process_id);
898898
term socket_wrapper = create_udp_socket_wrapper(socket_pid, &heap, glb);
@@ -961,7 +961,7 @@ static EventListener *passive_recvfrom_callback(GlobalContext *glb, EventListene
961961
term pid = listener->pid;
962962
term ref = term_from_ref_ticks(listener->ref_ticks, &heap);
963963
term addr = socket_heap_tuple_from_addr(&heap, htonl(clientaddr.sin_addr.s_addr));
964-
term port = term_from_int32(htons(clientaddr.sin_port));
964+
term port = term_from_int28(htons(clientaddr.sin_port));
965965
term packet = socket_create_packet_term(buf, len, socket_data->binary, &heap, glb);
966966
term addr_port_packet = port_heap_create_tuple3(&heap, addr, port, packet);
967967
term payload = port_heap_create_ok_tuple(&heap, addr_port_packet);

0 commit comments

Comments
 (0)