Skip to content

Commit 546cf24

Browse files
authored
Merge pull request eclipse-zenoh#812 from ZettaScaleLabs/asan_ci_tests
Add AddressSanitizer to CI tests
2 parents 8298a7a + b64706a commit 546cf24

13 files changed

+75
-26
lines changed

.github/workflows/build-check.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: Build & run tests
3131
run: |
3232
sudo apt install -y ninja-build
33-
CMAKE_GENERATOR=Ninja make test
33+
CMAKE_GENERATOR=Ninja ASAN=ON make test
3434
3535
check_format:
3636
name: Check codebase format with clang-format
@@ -290,6 +290,6 @@ jobs:
290290
- name: Build & test pico
291291
run: |
292292
sudo apt install -y ninja-build
293-
CMAKE_GENERATOR=Ninja make
293+
CMAKE_GENERATOR=Ninja ASAN=ON make
294294
python3 ./build/tests/no_router.py
295295
timeout-minutes: 5

CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ else()
9797
add_compile_options(-Wpedantic)
9898
endif()
9999
# add_compile_options(-Wconversion)
100-
# add_link_options(-fsanitize=address)
101100
elseif(MSVC)
102101
add_compile_options(/W4 /WX /Od /wd4127)
103102
elseif(CMAKE_SYSTEM_NAME MATCHES "Generic")
@@ -379,12 +378,14 @@ option(BUILD_EXAMPLES "Use this to also build the examples." ON)
379378
option(BUILD_TOOLS "Use this to also build the tools." OFF)
380379
option(BUILD_TESTING "Use this to also build tests." ON)
381380
option(BUILD_INTEGRATION "Use this to also build integration tests." OFF)
381+
option(ASAN "Enable AddressSanitizer." OFF)
382382

383383
message(STATUS "Produce Debian and RPM packages: ${PACKAGING}")
384384
message(STATUS "Build examples: ${BUILD_EXAMPLES}")
385385
message(STATUS "Build tools: ${BUILD_TOOLS}")
386386
message(STATUS "Build tests: ${BUILD_TESTING}")
387387
message(STATUS "Build integration: ${BUILD_INTEGRATION}")
388+
message(STATUS "AddressSanitizer: ${ASAN}")
388389

389390
set(PICO_LIBS "")
390391
if(PICO_STATIC)
@@ -453,6 +454,11 @@ if(BUILD_EXAMPLES)
453454
add_subdirectory(examples)
454455
endif()
455456

457+
if(ASAN)
458+
add_compile_options(-fsanitize=address)
459+
add_link_options(-fsanitize=address)
460+
endif()
461+
456462
if(UNIX OR MSVC)
457463
if(BUILD_TOOLS)
458464
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tools)

GNUmakefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ BUILD_TOOLS?=OFF
4141
# Accepted values: ON, OFF
4242
FORCE_C99?=OFF
4343

44+
# Enable AddressSanitizer.
45+
# Accepted values: ON, OFF
46+
ASAN?=OFF
47+
4448
# Debug level. This sets the ZENOH_DEBUG variable.
4549
# Accepted values:
4650
# 0: NONE
@@ -82,7 +86,7 @@ CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAK
8286
-DZ_FEATURE_MULTI_THREAD=$(Z_FEATURE_MULTI_THREAD) -DZ_FEATURE_INTEREST=$(Z_FEATURE_INTEREST) -DZ_FEATURE_UNSTABLE_API=$(Z_FEATURE_UNSTABLE_API)\
8387
-DZ_FEATURE_PUBLICATION=$(Z_FEATURE_PUBLICATION) -DZ_FEATURE_SUBSCRIPTION=$(Z_FEATURE_SUBSCRIPTION) -DZ_FEATURE_QUERY=$(Z_FEATURE_QUERY) -DZ_FEATURE_QUERYABLE=$(Z_FEATURE_QUERYABLE)\
8488
-DZ_FEATURE_RAWETH_TRANSPORT=$(Z_FEATURE_RAWETH_TRANSPORT) -DFRAG_MAX_SIZE=$(FRAG_MAX_SIZE) -DBATCH_UNICAST_SIZE=$(BATCH_UNICAST_SIZE) -DBATCH_MULTICAST_SIZE=$(BATCH_MULTICAST_SIZE)\
85-
-DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H.
89+
-DASAN=$(ASAN) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H.
8690

8791
ifeq ($(FORCE_C99), ON)
8892
CMAKE_OPT += -DCMAKE_C_STANDARD=99

src/collections/refcount.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ bool _z_rc_decrease_strong(void** cnt) {
198198
if (_ZP_RC_OP_DECR_AND_CMP_STRONG(c, 1)) {
199199
return _z_rc_decrease_weak(cnt);
200200
}
201-
return _z_rc_decrease_weak(cnt);
201+
_z_rc_decrease_weak(cnt);
202+
return true;
202203
}
203204

204205
bool _z_rc_decrease_weak(void** cnt) {

src/protocol/keyexpr.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ zp_keyexpr_canon_status_t __zp_canon_prefix(const char *start, size_t *len) {
146146
char const *next_slash;
147147

148148
do {
149-
next_slash = strchr(chunk_start, '/');
149+
next_slash = memchr(chunk_start, '/', _z_ptr_char_diff(end, chunk_start));
150150
const char *chunk_end = next_slash ? next_slash : end;
151151
size_t chunk_len = _z_ptr_char_diff(chunk_end, chunk_start);
152152
switch (chunk_len) {
@@ -291,7 +291,7 @@ void __zp_ke_write_chunk(char **writer, const char *chunk, size_t len, const cha
291291
writer[0] = _z_ptr_char_offset(writer[0], 1);
292292
}
293293

294-
(void)memcpy(writer[0], chunk, len);
294+
(void)memmove(writer[0], chunk, len);
295295
writer[0] = _z_ptr_char_offset(writer[0], (ptrdiff_t)len);
296296
}
297297

@@ -766,10 +766,9 @@ zp_keyexpr_canon_status_t _z_keyexpr_canonize(char *start, size_t *len) {
766766
} else {
767767
assert(false); // anything before "$*" or "**" must be part of the canon prefix
768768
}
769-
770769
while (next_slash != NULL) {
771770
reader = _z_ptr_char_offset(next_slash, 1);
772-
next_slash = strchr(reader, '/');
771+
next_slash = memchr(reader, '/', _z_ptr_char_diff(end, reader));
773772
chunk_end = next_slash ? next_slash : end;
774773
switch (_z_ptr_char_diff(chunk_end, reader)) {
775774
case 0: {

tests/z_api_encoding_test.c

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void test_with_schema(void) {
8585
z_encoding_to_string(z_encoding_loan_mut(&e), &s);
8686
assert(strncmp("zenoh/bytes;my_schema", z_string_data(z_string_loan(&s)), z_string_len(z_string_loan(&s))) == 0);
8787
z_encoding_drop(z_encoding_move(&e));
88+
z_string_drop(z_string_move(&s));
8889

8990
z_encoding_from_str(&e, "zenoh/string;");
9091
z_encoding_set_schema_from_substr(z_encoding_loan_mut(&e), "my_schema", 3);

tests/z_channels_test.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
//
1414
#include <assert.h>
1515
#include <stddef.h>
16-
#include <stdio.h>
17-
#include <stdlib.h>
1816

1917
#include "zenoh-pico/api/handlers.h"
2018
#include "zenoh-pico/api/macros.h"
21-
#include "zenoh-pico/net/sample.h"
19+
#include "zenoh-pico/collections/bytes.h"
2220

2321
#undef NDEBUG
2422
#include <assert.h>
@@ -37,6 +35,7 @@
3735
.attachment = _z_bytes_null(), \
3836
}; \
3937
z_call(*z_loan(closure), &sample); \
38+
_z_bytes_drop(&payload); \
4039
} while (0);
4140

4241
#define _RECV(handler, method, buf) \
@@ -192,11 +191,13 @@ void zero_size_test(void) {
192191
assert(z_fifo_channel_sample_new(&closure, &fifo_handler, 0) != Z_OK);
193192
assert(z_fifo_channel_sample_new(&closure, &fifo_handler, 1) == Z_OK);
194193
z_drop(z_move(fifo_handler));
194+
z_drop(z_move(closure));
195195

196196
z_owned_ring_handler_sample_t ring_handler;
197197
assert(z_ring_channel_sample_new(&closure, &ring_handler, 0) != Z_OK);
198198
assert(z_ring_channel_sample_new(&closure, &ring_handler, 1) == Z_OK);
199199
z_drop(z_move(ring_handler));
200+
z_drop(z_move(closure));
200201
}
201202

202203
int main(void) {

tests/z_collections_test.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include "zenoh-pico/collections/fifo.h"
2020
#include "zenoh-pico/collections/lifo.h"
21-
#include "zenoh-pico/collections/list.h"
2221
#include "zenoh-pico/collections/ring.h"
2322
#include "zenoh-pico/collections/string.h"
2423

@@ -313,10 +312,10 @@ void int_map_iterator_test(void) {
313312
_z_str_intmap_t map;
314313

315314
map = _z_str_intmap_make();
316-
_z_str_intmap_insert(&map, 10, "A");
317-
_z_str_intmap_insert(&map, 20, "B");
318-
_z_str_intmap_insert(&map, 30, "C");
319-
_z_str_intmap_insert(&map, 40, "D");
315+
_z_str_intmap_insert(&map, 10, _z_str_clone("A"));
316+
_z_str_intmap_insert(&map, 20, _z_str_clone("B"));
317+
_z_str_intmap_insert(&map, 30, _z_str_clone("C"));
318+
_z_str_intmap_insert(&map, 40, _z_str_clone("D"));
320319

321320
#define TEST_MAP(map) \
322321
{ \
@@ -346,6 +345,9 @@ void int_map_iterator_test(void) {
346345

347346
TEST_MAP(map2);
348347

348+
_z_str_intmap_clear(&map);
349+
_z_str_intmap_clear(&map2);
350+
349351
#undef TEST_MAP
350352
}
351353

tests/z_data_struct_test.c

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <stddef.h>
1616
#include <stdio.h>
1717
#include <stdlib.h>
18+
#include <string.h>
1819

1920
#include "zenoh-pico/api/primitives.h"
2021
#include "zenoh-pico/api/types.h"
@@ -29,6 +30,7 @@ void entry_list_test(void) {
2930
_z_transport_peer_entry_list_t *root = _z_transport_peer_entry_list_new();
3031
for (int i = 0; i < 10; i++) {
3132
_z_transport_peer_entry_t *entry = (_z_transport_peer_entry_t *)z_malloc(sizeof(_z_transport_peer_entry_t));
33+
memset(entry, 0, sizeof(_z_transport_peer_entry_t));
3234
root = _z_transport_peer_entry_list_insert(root, entry);
3335
}
3436
_z_transport_peer_entry_list_t *list = root;
@@ -39,6 +41,7 @@ void entry_list_test(void) {
3941

4042
for (int i = 0; i < 11; i++) {
4143
_z_transport_peer_entry_t *entry = (_z_transport_peer_entry_t *)z_malloc(sizeof(_z_transport_peer_entry_t));
44+
memset(entry, 0, sizeof(_z_transport_peer_entry_t));
4245
root = _z_transport_peer_entry_list_insert(root, entry);
4346
}
4447
assert(_z_transport_peer_entry_list_head(root)->_peer_id == _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE - 1);
@@ -140,6 +143,8 @@ void str_vec_list_intmap_test(void) {
140143

141144
_z_str_intmap_clear(&map);
142145
assert(_z_str_intmap_is_empty(&map) == true);
146+
147+
z_free(s);
143148
}
144149

145150
void _z_slice_custom_deleter(void *data, void *context) {
@@ -243,6 +248,7 @@ void z_id_to_string_test(void) {
243248
assert(z_string_len(z_string_loan(&id_str)) == 32);
244249
assert(strncmp("0f0e0d0c0b0a09080706050403020100", z_string_data(z_string_loan(&id_str)),
245250
z_string_len(z_string_loan(&id_str))) == 0);
251+
z_string_drop(z_string_move(&id_str));
246252
}
247253

248254
int main(void) {

tests/z_endpoint_test.c

+17
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,32 @@ int main(void) {
4242

4343
str = _z_string_alias_str("");
4444
assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
45+
_z_locator_clear(&lc);
4546

4647
str = _z_string_alias_str("/");
4748
assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
49+
_z_locator_clear(&lc);
4850

4951
str = _z_string_alias_str("tcp");
5052
assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
53+
_z_locator_clear(&lc);
5154

5255
str = _z_string_alias_str("tcp/");
5356
assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
57+
_z_locator_clear(&lc);
5458

5559
str = _z_string_alias_str("127.0.0.1:7447");
5660
assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
61+
_z_locator_clear(&lc);
5762

5863
str = _z_string_alias_str("tcp/127.0.0.1:7447?");
5964
assert(_z_locator_from_string(&lc, &str) == _Z_RES_OK);
65+
_z_locator_clear(&lc);
6066

6167
// No metadata defined so far... but this is a valid syntax in principle
6268
str = _z_string_alias_str("tcp/127.0.0.1:7447?invalid=ctrl");
6369
assert(_z_locator_from_string(&lc, &str) == _Z_RES_OK);
70+
_z_locator_clear(&lc);
6471

6572
// Endpoint
6673
printf(">>> Testing endpoints...\n");
@@ -80,25 +87,32 @@ int main(void) {
8087

8188
str = _z_string_alias_str("");
8289
assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
90+
_z_endpoint_clear(&ep);
8391

8492
str = _z_string_alias_str("/");
8593
assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
94+
_z_endpoint_clear(&ep);
8695

8796
str = _z_string_alias_str("tcp");
8897
assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
98+
_z_endpoint_clear(&ep);
8999

90100
str = _z_string_alias_str("tcp/");
91101
assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
102+
_z_endpoint_clear(&ep);
92103

93104
str = _z_string_alias_str("127.0.0.1:7447");
94105
assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID);
106+
_z_endpoint_clear(&ep);
95107

96108
str = _z_string_alias_str("tcp/127.0.0.1:7447?");
97109
assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK);
110+
_z_endpoint_clear(&ep);
98111

99112
// No metadata defined so far... but this is a valid syntax in principle
100113
str = _z_string_alias_str("tcp/127.0.0.1:7447?invalid=ctrl");
101114
assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK);
115+
_z_endpoint_clear(&ep);
102116

103117
str = _z_string_alias_str("udp/127.0.0.1:7447#iface=eth0");
104118
assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK);
@@ -116,12 +130,15 @@ int main(void) {
116130

117131
str = _z_string_alias_str("udp/127.0.0.1:7447#invalid=eth0");
118132
assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK);
133+
_z_endpoint_clear(&ep);
119134

120135
str = _z_string_alias_str("udp/127.0.0.1:7447?invalid=ctrl#iface=eth0");
121136
assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK);
137+
_z_endpoint_clear(&ep);
122138

123139
str = _z_string_alias_str("udp/127.0.0.1:7447?invalid=ctrl#invalid=eth0");
124140
assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK);
141+
_z_endpoint_clear(&ep);
125142

126143
return 0;
127144
}

tests/z_keyexpr_test.c

+2
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ void test_canonize(void) {
314314
printf(" Match: %.*s : %s\n", (int)canon_len, canon, canonized[i]);
315315
assert(strncmp(canonized[i], canon, canon_len) == 0);
316316
}
317+
free(canon);
317318
}
318319

319320
for (int i = 0; i < N; i++) {
@@ -331,6 +332,7 @@ void test_canonize(void) {
331332
assert(strncmp(canonized[i], canon, canon_len) == 0);
332333
}
333334
printf("\n");
335+
free(canon);
334336
}
335337
}
336338

tests/z_msgcodec_test.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,23 @@ char *gen_str(size_t size) {
221221
return str;
222222
}
223223

224+
_z_string_t gen_string(size_t len) {
225+
char *str = gen_str(len);
226+
_z_string_t ret = _z_string_copy_from_str(str);
227+
z_free(str);
228+
return ret;
229+
}
230+
224231
_z_string_svec_t gen_str_array(size_t size) {
225232
_z_string_svec_t sa = _z_string_svec_make(size);
226233
for (size_t i = 0; i < size; i++) {
227-
_z_string_t s = _z_string_copy_from_str(gen_str(16));
234+
_z_string_t s = gen_string(16);
228235
_z_string_svec_append(&sa, &s);
229236
}
230237

231238
return sa;
232239
}
233240

234-
_z_string_t gen_string(size_t len) { return _z_string_alias_str(gen_str(len)); }
235-
236241
_z_locator_array_t gen_locator_array(size_t size) {
237242
_z_locator_array_t la = _z_locator_array_make(size);
238243
for (size_t i = 0; i < size; i++) {
@@ -341,7 +346,7 @@ void assert_eq_locator_array(const _z_locator_array_t *left, const _z_locator_ar
341346
_z_string_t ls = _z_locator_to_string(l);
342347
_z_string_t rs = _z_locator_to_string(r);
343348

344-
printf("%s:%s", _z_string_data(&ls), _z_string_data(&rs));
349+
printf("%.*s:%.*s", (int)_z_string_len(&ls), _z_string_data(&ls), (int)_z_string_len(&rs), _z_string_data(&rs));
345350
if (i < left->_len - 1) printf(" ");
346351

347352
_z_string_clear(&ls);

0 commit comments

Comments
 (0)