Skip to content

Commit 0e1e180

Browse files
authored
Merge pull request #11288 from garazdawi/lukas/erts/fixes
A bucket of various erts bug fixes OTP-20314
2 parents 053f80e + 4283eea commit 0e1e180

30 files changed

Lines changed: 198 additions & 82 deletions

erts/emulator/beam/atom.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ erts_atom_put_index(const byte *name, Sint len, ErtsAtomEncoding enc, int trunc)
282282
/* First sanity check; need to verify later */
283283
if (tlen > MAX_ATOM_SZ_LIMIT && !trunc)
284284
return ATOM_MAX_CHARS_ERROR;
285+
if (tlen > ERTS_SINT16_MAX)
286+
return ATOM_MAX_CHARS_ERROR;
285287
break;
286288
}
287289

erts/emulator/beam/beam_common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ apply(Process* p, Eterm* reg, ErtsCodePtr I, Uint stack_offset)
15031503
tmp = args;
15041504
arity = 0;
15051505
while (is_list(tmp)) {
1506-
if (arity < (MAX_REG - 1)) {
1506+
if (arity < MAX_ARG) {
15071507
reg[arity++] = CAR(list_val(tmp));
15081508
tmp = CDR(list_val(tmp));
15091509
} else {

erts/emulator/beam/beam_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ static int parse_atom_chunk(BeamFile *beam,
288288
}
289289

290290
LoadAssert(beamreader_read_bytes(&reader, length, &string));
291-
atom = erts_atom_put(string, length, ERTS_ATOM_ENC_UTF8, 1);
291+
atom = erts_atom_put(string, length, ERTS_ATOM_ENC_UTF8, 0);
292292
LoadAssert(atom != THE_NON_VALUE);
293293

294294
atoms->entries[i] = atom;

erts/emulator/beam/bif.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,16 +1366,16 @@ BIF_RETTYPE hibernate_3(BIF_ALIST_3)
13661366
BIF_ERROR(BIF_P, BADARG);
13671367
}
13681368

1369-
while (is_list(args) && arity <= MAX_ARG) {
1370-
args = CDR(list_val(args));
1371-
arity++;
1372-
}
1373-
1374-
if (is_not_nil(args)) {
1375-
if (arity > MAX_ARG) {
1369+
while (is_list(args)) {
1370+
if (arity < MAX_ARG) {
1371+
args = CDR(list_val(args));
1372+
arity++;
1373+
} else {
13761374
BIF_ERROR(BIF_P, SYSTEM_LIMIT);
13771375
}
1376+
}
13781377

1378+
if (is_not_nil(args)) {
13791379
BIF_ERROR(BIF_P, BADARG);
13801380
}
13811381

@@ -4742,7 +4742,8 @@ BIF_RETTYPE list_to_ref_1(BIF_ALIST_1)
47424742
n++;
47434743
if (ints[i] > ~((Uint32) 0)) goto bad;
47444744
if (*cp == '>') break;
4745-
if (*cp++ != '.') goto bad;
4745+
/* We don't find a ., or we are on the last position and do find a dot */
4746+
if (*cp++ != '.' || i == sizeof(ints)/sizeof(Uint) - 1) goto bad;
47464747
}
47474748

47484749
if (*cp++ != '>') goto bad;

erts/emulator/beam/big.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3984,13 +3984,13 @@ static ERTS_INLINE byte c2int_digit_from_base(byte ch) {
39843984

39853985
/*
39863986
* How many bits are needed to store 1 digit of given base in binary
3987-
* Wo.Alpha formula: Table [log2[n], {n,2,36}]
3987+
* Wo.Alpha formula: Table[Floor[10^5 * Log2[n]] / 10^5, {n,2,36}]
39883988
*/
39893989
static const double lg2_lookup[36-1] = {
3990-
1.0, 1.58496, 2.0, 2.32193, 2.58496, 2.80735, 3.0, 3.16993, 3.32193,
3991-
3.45943, 3.58496, 3.70044, 3.80735, 3.90689, 4.0, 4.08746, 4.16993, 4.24793,
3992-
4.32193, 4.39232, 4.45943, 4.52356, 4.58496, 4.64386, 4.70044, 4.75489,
3993-
4.80735, 4.85798, 4.90689, 4.9542, 5.0, 5.04439, 5.08746, 5.12928, 5.16993
3990+
1.0, 1.58496, 2.0, 2.32192, 2.58496, 2.80735, 3.0, 3.16992, 3.32192,
3991+
3.45943, 3.58496, 3.70043, 3.80735, 3.90689, 4.0, 4.08746, 4.16992, 4.24792,
3992+
4.32192, 4.39231, 4.45943, 4.52356, 4.58496, 4.64385, 4.70043, 4.75488,
3993+
4.80735, 4.85798, 4.90689, 4.95419, 5, 5.04439, 5.08746, 5.12928, 5.16992
39943994
};
39953995

39963996
/*

erts/emulator/beam/dist.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,9 @@ int erts_net_message(Port *prt,
24932493
token = NIL;
24942494
} else {
24952495
token = tuple[5];
2496+
2497+
if (!SEQ_TRACE_T_VALID(token))
2498+
goto invalid_message;
24962499
}
24972500

24982501
erts_queue_dist_message(rp, locks, edep, ede_hfrag, token, from);
@@ -2513,6 +2516,10 @@ int erts_net_message(Port *prt,
25132516
}
25142517

25152518
token = tuple[4];
2519+
2520+
if (!SEQ_TRACE_T_VALID(token))
2521+
goto invalid_message;
2522+
25162523
goto send_common;
25172524

25182525
case DOP_SEND_SENDER:
@@ -2563,6 +2570,9 @@ int erts_net_message(Port *prt,
25632570
if (tuple_arity != 4)
25642571
goto invalid_message;
25652572
token = tuple[4];
2573+
2574+
if (!SEQ_TRACE_T_VALID(token))
2575+
goto invalid_message;
25662576
}
25672577

25682578
#ifdef ERTS_DIST_MSG_DBG
@@ -2646,6 +2656,8 @@ int erts_net_message(Port *prt,
26462656
else {
26472657
token = tuple[5];
26482658
}
2659+
if (!SEQ_TRACE_T_VALID(token))
2660+
goto invalid_message;
26492661
if ((flags & ERTS_DOP_ALTACT_SIG_FLG_ALIAS)) {
26502662
if (is_not_ref(to) || (flags & ERTS_DOP_ALTACT_SIG_FLG_NAME)) {
26512663
goto invalid_message;
@@ -2783,6 +2795,9 @@ int erts_net_message(Port *prt,
27832795
goto invalid_message;
27842796
}
27852797

2798+
if (!SEQ_TRACE_T_VALID(token))
2799+
goto invalid_message;
2800+
27862801
if (!erts_proc_lookup(to)) {
27872802
if (ede_hfrag != NULL) {
27882803
erts_free_dist_ext_copy(erts_get_dist_ext(ede_hfrag));
@@ -2842,6 +2857,8 @@ int erts_net_message(Port *prt,
28422857
|| dep != external_pid_dist_entry(from)) {
28432858
goto invalid_message;
28442859
}
2860+
if (!SEQ_TRACE_T_VALID(token))
2861+
goto invalid_message;
28452862
if (is_not_internal_pid(to)) {
28462863
if (is_external_pid(to)) {
28472864
DistEntry *dep = external_pid_dist_entry(to);
@@ -2893,6 +2910,9 @@ int erts_net_message(Port *prt,
28932910

28942911
token = tuple[7];
28952912

2913+
if (!SEQ_TRACE_T_VALID(token))
2914+
goto invalid_message;
2915+
28962916
if (0) {
28972917

28982918
case DOP_SPAWN_REQUEST:
@@ -3023,6 +3043,9 @@ int erts_net_message(Port *prt,
30233043

30243044
token = tuple[6];
30253045

3046+
if (!SEQ_TRACE_T_VALID(token))
3047+
goto invalid_message;
3048+
30263049
if (0) {
30273050
case DOP_SPAWN_REPLY:
30283051

erts/emulator/beam/emu/msg_instrs.tab

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,7 @@ remove_message() {
185185
DT_UTAG_FLAGS(c_p) |= DT_UTAG_SPREADING;
186186
} else {
187187
#endif
188-
ASSERT(is_tuple(SEQ_TRACE_TOKEN(c_p)));
189-
ASSERT(SEQ_TRACE_TOKEN_ARITY(c_p) == 5);
190-
ASSERT(is_small(SEQ_TRACE_TOKEN_SERIAL(c_p)));
191-
ASSERT(is_small(SEQ_TRACE_TOKEN_LASTCNT(c_p)));
192-
ASSERT(is_small(SEQ_TRACE_TOKEN_FLAGS(c_p)));
193-
ASSERT(is_pid(SEQ_TRACE_TOKEN_SENDER(c_p))
194-
|| is_atom(SEQ_TRACE_TOKEN_SENDER(c_p)));
188+
ASSERT(SEQ_TRACE_TOKEN_VALID(c_p));
195189
c_p->seq_trace_lastcnt = unsigned_val(SEQ_TRACE_TOKEN_SERIAL(c_p));
196190
if (c_p->seq_trace_clock < unsigned_val(SEQ_TRACE_TOKEN_SERIAL(c_p))) {
197191
c_p->seq_trace_clock = unsigned_val(SEQ_TRACE_TOKEN_SERIAL(c_p));

erts/emulator/beam/erl_bif_atomics.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,19 @@ BIF_RETTYPE erts_internal_atomics_new_2(BIF_ALIST_2)
7171
BIF_ERROR(BIF_P, opts);
7272
}
7373

74-
if (cnt > (ERTS_UWORD_MAX / sizeof(p->v[0])))
74+
/* Check that cnt*sizeof(UWord) fits into a word */
75+
if (cnt > (ERTS_UWORD_MAX / sizeof(p->v[0]))) {
7576
BIF_ERROR(BIF_P, SYSTEM_LIMIT);
77+
}
78+
79+
bytes = cnt*sizeof(p->v[0]);
80+
81+
/* Check if adding bytes makes it wrap around */
82+
if (bytes + offsetof(AtomicsRef, v) < bytes) {
83+
BIF_ERROR(BIF_P, SYSTEM_LIMIT);
84+
}
7685

77-
bytes = offsetof(AtomicsRef, v) + cnt*sizeof(p->v[0]);
86+
bytes += offsetof(AtomicsRef, v);
7887
mbin = erts_create_magic_binary_x(bytes,
7988
atomics_destructor,
8089
ERTS_ALC_T_ATOMICS,

erts/emulator/beam/erl_db.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,7 @@ do_update_counter(Process *p, DbTable* tb,
15081508
Eterm* htop; /* actual heap usage */
15091509
Eterm* hstart;
15101510
Eterm* hend;
1511+
Uint largest_big_arity = 0;
15111512
ERTS_UNDEF(ret, THE_NON_VALUE);
15121513

15131514
UseTmpHeap(5, p);
@@ -1573,10 +1574,10 @@ do_update_counter(Process *p, DbTable* tb,
15731574
goto finalize;
15741575
}
15751576
incr = tpl[2];
1576-
if (is_big(incr)) {
1577-
halloc_size += BIG_NEED_SIZE(big_arity(incr));
1577+
if (is_big(incr) && largest_big_arity < big_arity(incr)) {
1578+
largest_big_arity = big_arity(incr);
15781579
}
1579-
else if (is_not_small(incr)) {
1580+
else if (is_not_integer(incr)) {
15801581
goto finalize;
15811582
}
15821583
position = signed_val(tpl[1]);
@@ -1591,12 +1592,16 @@ do_update_counter(Process *p, DbTable* tb,
15911592
goto finalize;
15921593
}
15931594
oldcnt = db_do_read_element(&handle, position);
1594-
if (is_big(oldcnt)) {
1595-
halloc_size += BIG_NEED_SIZE(big_arity(oldcnt));
1595+
if (is_big(oldcnt) && largest_big_arity < big_arity(oldcnt)) {
1596+
largest_big_arity = big_arity(oldcnt);
15961597
}
1597-
else if (is_not_small(oldcnt)) {
1598+
else if (is_not_integer(oldcnt)) {
15981599
goto finalize;
15991600
}
1601+
if (largest_big_arity > 0) {
1602+
halloc_size += BIG_NEED_SIZE(largest_big_arity);
1603+
}
1604+
16001605
break;
16011606
default:
16021607
goto finalize;

erts/emulator/beam/erl_db_util.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2810,10 +2810,7 @@ Eterm db_prog_match(Process *c_p,
28102810
Eterm token;
28112811
Uint token_sz;
28122812

2813-
ASSERT(SEQ_TRACE_TOKEN_ARITY(c_p) == 5);
2814-
ASSERT(is_immed(SEQ_TRACE_TOKEN_FLAGS(c_p)));
2815-
ASSERT(is_immed(SEQ_TRACE_TOKEN_SERIAL(c_p)));
2816-
ASSERT(is_immed(SEQ_TRACE_TOKEN_LASTCNT(c_p)));
2813+
ASSERT(SEQ_TRACE_TOKEN_VALID(c_p));
28172814

28182815
token = SEQ_TRACE_TOKEN(c_p);
28192816
token_sz = size_object(token);

0 commit comments

Comments
 (0)