Skip to content

Commit 58fac53

Browse files
committed
refactor: Add a bin_unpack_bin_max for max-length arrays.
These are statically allocated (e.g. `uint8_t[1024]`) arrays with variable length data inside them. Examples are group topics and nicknames.
1 parent 6be29f0 commit 58fac53

27 files changed

+110
-62
lines changed

.circleci/bazel-test

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ git submodule update --init --recursive
66
/src/workspace/tools/inject-repo c-toxcore
77
# TODO(iphydf): Re-enable fuzz-test when https://github.com/tweag/rules_nixpkgs/issues/442 is fixed.
88
cd /src/workspace && bazel test -k \
9-
--config=ci \
10-
--config=remote \
119
--build_tag_filters=-haskell,-fuzz-test \
1210
--test_tag_filters=-haskell,-fuzz-test \
1311
-- \
14-
//c-toxcore/... \
1512
"$@"

.circleci/config.yml

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ workflows:
77
jobs:
88
# Dynamic analysis in the Bazel build
99
- bazel-asan
10+
- bazel-msan
1011
- bazel-tsan
1112
# Dynamic analysis with CMake
1213
- asan
1314
- tsan
14-
- msan
1515
- ubsan
1616
# Static analysis
1717
- clang-analyze
@@ -29,6 +29,7 @@ jobs:
2929
steps:
3030
- checkout
3131
- run: .circleci/bazel-test
32+
//c-toxcore/...
3233

3334
bazel-tsan:
3435
working_directory: /tmp/cirrus-ci-build
@@ -38,11 +39,22 @@ jobs:
3839
steps:
3940
- checkout
4041
- run: .circleci/bazel-test
42+
//c-toxcore/...
4143
-//c-toxcore/auto_tests:conference_av_test
4244
-//c-toxcore/auto_tests:conference_test
4345
-//c-toxcore/auto_tests:onion_test
4446
-//c-toxcore/auto_tests:tox_many_test
4547

48+
bazel-msan:
49+
working_directory: /tmp/cirrus-ci-build
50+
docker:
51+
- image: toxchat/toktok-stack:latest-msan
52+
53+
steps:
54+
- checkout
55+
- run: .circleci/bazel-test
56+
//c-toxcore/auto_tests:lossless_packet_test
57+
4658
asan:
4759
working_directory: ~/work
4860
docker:
@@ -91,21 +103,6 @@ jobs:
91103
- run: git submodule update --init --recursive
92104
- run: CC=clang .circleci/cmake-ubsan
93105

94-
msan:
95-
working_directory: ~/work
96-
docker:
97-
- image: toxchat/toktok-stack:latest-msan
98-
99-
steps:
100-
- checkout
101-
- run: git submodule update --init --recursive
102-
- run: rm -rf /src/workspace/c-toxcore/* && mv * /src/workspace/c-toxcore/
103-
- run:
104-
cd /src/workspace && bazel test
105-
//c-toxcore/auto_tests:lossless_packet_test
106-
//c-toxcore/toxav/...
107-
//c-toxcore/toxcore/...
108-
109106
infer:
110107
working_directory: ~/work
111108
docker:

auto_tests/BUILD.bazel

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,26 @@ flaky_tests = {
2929
"tox_many_tcp_test": True,
3030
}
3131

32+
extra_args = {
33+
"proxy_test": ["$(location //c-toxcore/other/proxy)"],
34+
}
35+
36+
extra_data = {
37+
"proxy_test": ["//c-toxcore/other/proxy"],
38+
}
39+
3240
[cc_test(
3341
name = src[:-2],
3442
size = "small",
3543
srcs = [src],
36-
args = ["$(location %s)" % src] + ["$(location //c-toxcore/other/proxy)"],
37-
data = glob(["data/*"]) + ["//c-toxcore/other/proxy"],
44+
args = ["$(location %s)" % src] + extra_args.get(
45+
src[:-2],
46+
[],
47+
),
48+
data = glob(["data/*"]) + extra_data.get(
49+
src[:-2],
50+
[],
51+
),
3852
flaky = flaky_tests.get(
3953
src[:-2],
4054
False,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
036adfc1e993624ae0bf49f08c2890bb44e6d4224a07a8c7fd2e2b5a8be6bf4c /usr/local/bin/tox-bootstrapd
1+
c71f87c6ff30393d748bbdc118248eff90a4874cfa015b3113534f2333154555 /usr/local/bin/tox-bootstrapd

toxcore/Messenger.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,9 +3220,13 @@ static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t
32203220

32213221
if (group_number < 0) {
32223222
LOGGER_WARNING(m->log, "Failed to load group %u", i);
3223+
// Can't recover trivially. We may need to skip over some data here.
3224+
break;
32233225
}
32243226
}
32253227

3228+
LOGGER_DEBUG(m->log, "Successfully loaded %u groups", gc_count_groups(m->group_handler));
3229+
32263230
bin_unpack_free(bu);
32273231

32283232
return STATE_LOAD_STATUS_CONTINUE;

toxcore/bin_unpack.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,14 @@ bool bin_unpack_array(Bin_Unpack *bu, uint32_t *size)
7373
return cmp_read_array(&bu->ctx, size) && *size <= bu->bytes_size;
7474
}
7575

76-
bool bin_unpack_array_fixed(Bin_Unpack *bu, uint32_t required_size)
76+
bool bin_unpack_array_fixed(Bin_Unpack *bu, uint32_t required_size, uint32_t *actual_size)
7777
{
78-
uint32_t size;
79-
return cmp_read_array(&bu->ctx, &size) && size == required_size;
78+
uint32_t size = 0;
79+
const bool success = cmp_read_array(&bu->ctx, &size) && size == required_size;
80+
if (actual_size != nullptr) {
81+
*actual_size = size;
82+
}
83+
return success;
8084
}
8185

8286
bool bin_unpack_bool(Bin_Unpack *bu, bool *val)
@@ -128,6 +132,18 @@ bool bin_unpack_bin(Bin_Unpack *bu, uint8_t **data_ptr, uint32_t *data_length_pt
128132
return true;
129133
}
130134

135+
bool bin_unpack_bin_max(Bin_Unpack *bu, uint8_t *data, uint16_t *data_length_ptr, uint16_t max_data_length)
136+
{
137+
uint32_t bin_size;
138+
if (!bin_unpack_bin_size(bu, &bin_size) || bin_size > max_data_length) {
139+
return false;
140+
}
141+
142+
*data_length_ptr = bin_size;
143+
144+
return bin_unpack_bin_b(bu, data, bin_size);
145+
}
146+
131147
bool bin_unpack_bin_fixed(Bin_Unpack *bu, uint8_t *data, uint32_t data_length)
132148
{
133149
uint32_t bin_size;

toxcore/bin_unpack.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ void bin_unpack_free(Bin_Unpack *bu);
4545
non_null() bool bin_unpack_array(Bin_Unpack *bu, uint32_t *size);
4646

4747
/** @brief Start unpacking a fixed size MessagePack array.
48+
*
49+
* Fails if the array size is not the required size. If `actual_size` is passed a non-null
50+
* pointer, the array size is written there.
4851
*
4952
* @retval false if the packed array size is not exactly the required size.
5053
*/
51-
non_null() bool bin_unpack_array_fixed(Bin_Unpack *bu, uint32_t required_size);
54+
non_null(1) nullable(3)
55+
bool bin_unpack_array_fixed(Bin_Unpack *bu, uint32_t required_size, uint32_t *actual_size);
5256

5357
/** @brief Unpack a MessagePack bool. */
5458
non_null() bool bin_unpack_bool(Bin_Unpack *bu, bool *val);
@@ -71,10 +75,16 @@ non_null() bool bin_unpack_nil(Bin_Unpack *bu);
7175
* large allocation unless the input array was already that large.
7276
*/
7377
non_null() bool bin_unpack_bin(Bin_Unpack *bu, uint8_t **data_ptr, uint32_t *data_length_ptr);
78+
/** @brief Unpack a variable size MessagePack bin into a fixed size byte array.
79+
*
80+
* Stores unpacked data into `data` with its length stored in `data_length_ptr`. This function does
81+
* not allocate memory and requires that `max_data_length` is less than or equal to `sizeof(arr)`
82+
* when `arr` is passed as `data` pointer.
83+
*/
84+
non_null() bool bin_unpack_bin_max(Bin_Unpack *bu, uint8_t *data, uint16_t *data_length_ptr, uint16_t max_data_length);
7485
/** @brief Unpack a MessagePack bin of a fixed length into a pre-allocated byte array.
7586
*
76-
* Unlike the function above, this function does not allocate any memory, but requires the size to
77-
* be known up front.
87+
* Similar to the function above, but doesn't output the data length.
7888
*/
7989
non_null() bool bin_unpack_bin_fixed(Bin_Unpack *bu, uint8_t *data, uint32_t data_length);
8090

toxcore/events/conference_invite.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static bool tox_event_conference_invite_unpack(
120120
Tox_Event_Conference_Invite *event, Bin_Unpack *bu)
121121
{
122122
assert(event != nullptr);
123-
if (!bin_unpack_array_fixed(bu, 3)) {
123+
if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
124124
return false;
125125
}
126126

toxcore/events/conference_message.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static bool tox_event_conference_message_unpack(
135135
Tox_Event_Conference_Message *event, Bin_Unpack *bu)
136136
{
137137
assert(event != nullptr);
138-
if (!bin_unpack_array_fixed(bu, 4)) {
138+
if (!bin_unpack_array_fixed(bu, 4, nullptr)) {
139139
return false;
140140
}
141141

toxcore/events/conference_peer_name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static bool tox_event_conference_peer_name_unpack(
120120
Tox_Event_Conference_Peer_Name *event, Bin_Unpack *bu)
121121
{
122122
assert(event != nullptr);
123-
if (!bin_unpack_array_fixed(bu, 3)) {
123+
if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
124124
return false;
125125
}
126126

0 commit comments

Comments
 (0)