Skip to content

Commit b65bf3c

Browse files
authored
Fix misaligned access and make tests free memory (#274)
* fix potential misaligned access and make tests free memory for valgrind * use alignas()
1 parent 5676efa commit b65bf3c

File tree

9 files changed

+34
-11
lines changed

9 files changed

+34
-11
lines changed

include/udx.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern "C" {
66
#endif
77

88
#include <math.h>
9+
#include <stdalign.h>
910
#include <stdbool.h>
1011
#include <stdint.h>
1112
#include <uv.h>
@@ -428,7 +429,8 @@ struct udx_packet_s {
428429
bool is_app_limited; // was throughput app-limited (vs network limited) at the time the packet was transmitted?
429430

430431
// just alloc it in place here, easier to manage
431-
uint8_t header[UDX_HEADER_SIZE];
432+
alignas(4) uint8_t header[UDX_HEADER_SIZE];
433+
432434
uint16_t nwbufs; // nwbufs = nbufs - 1
433435
uint16_t nwbufs_capacity; // initially ARRAY_SIZEOF(wbuf_sml), used for realloc
434436

src/udx.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,8 @@ send_probe (udx_stream_t *stream) {
489489

490490
// todo: send a data packet with seq=remote_acked-1 instead
491491

492-
uint8_t header[20];
492+
alignas(4) uint8_t header[20];
493+
493494
udx_write_header(header, stream, UDX_HEADER_HEARTBEAT);
494495

495496
// fast path
@@ -2655,7 +2656,9 @@ stream_on_destroy_send (udx_stream_t *stream) {
26552656

26562657
static void
26572658
_stream_on_destroy_send (uv_udp_send_t *req, int status) {
2658-
debug_printf("udx destroy send: err=%s\n", uv_strerror(status));
2659+
if (status < 0) {
2660+
debug_printf("udx destroy send: err=%s\n", uv_strerror(status));
2661+
}
26592662
udx_stream_t *stream = req->data;
26602663
stream_on_destroy_send(stream);
26612664
free(req);
@@ -2682,7 +2685,8 @@ udx_stream_destroy (udx_stream_t *stream) {
26822685

26832686
// write destroy packet
26842687

2685-
uint8_t header[20];
2688+
alignas(4) uint8_t header[20];
2689+
26862690
udx_write_header(header, stream, UDX_HEADER_DESTROY);
26872691
stream->seq++;
26882692

test/stream-bbr-state.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,13 @@ send_periodic (uv_timer_t *timer) {
166166
static void
167167
send_ack_slow (udx_stream_write_t *r, int status, int unordered) {
168168
// printf("send_ack (slow)\n");
169+
free(r);
169170
}
170171

171172
static void
172173
send_ack_fast (udx_stream_write_t *r, int status, int unordered) {
173174
// printf("send_ack (fast) elapsed=%ld\n", uv_now(&loop) - start_ms);
175+
free(r);
174176
}
175177

176178
static void

test/stream-change-remote.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ main () {
162162
e = udx_stream_connect(&dstream, &dsock, 3, (struct sockaddr *) &caddr);
163163
assert(e == 0);
164164

165-
uv_buf_t buf = uv_buf_init(malloc(NBYTES_TO_SEND), NBYTES_TO_SEND);
165+
char *data = malloc(NBYTES_TO_SEND);
166+
167+
uv_buf_t buf = uv_buf_init(data, NBYTES_TO_SEND);
166168

167169
write_hash = hash(write_hash, (uint8_t *) buf.base, buf.len);
168170

@@ -177,6 +179,7 @@ main () {
177179
printf("read_hash=%lu write_hash=%lu\n", read_hash, write_hash);
178180

179181
free(req);
182+
free(data);
180183

181184
return 0;
182185
}

test/stream-destroy-before-connect.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ main () {
2020
e = udx_stream_destroy(&stream);
2121
assert(e == 0);
2222

23+
e = uv_run(&loop, UV_RUN_DEFAULT);
24+
assert(e == 0);
25+
2326
uv_loop_close(&loop);
2427

2528
return 0;

test/stream-relay-force-slow-path.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ main () {
133133
e = udx_stream_connect(&dstream, &dsock, 3, (struct sockaddr *) &caddr);
134134
assert(e == 0);
135135

136-
uv_buf_t buf = uv_buf_init(calloc(NBYTES_TO_SEND, 1), NBYTES_TO_SEND);
136+
char *data = calloc(NBYTES_TO_SEND, 1);
137+
138+
uv_buf_t buf = uv_buf_init(data, NBYTES_TO_SEND);
137139

138140
memcpy(buf.base, "hello", 5);
139141

@@ -148,6 +150,7 @@ main () {
148150
assert(nbytes_read == NBYTES_TO_SEND && read_hash == write_hash);
149151

150152
free(req);
153+
free(data);
151154

152155
return 0;
153156
}

test/stream-relay.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ main () {
131131
e = udx_stream_connect(&dstream, &dsock, 3, (struct sockaddr *) &caddr);
132132
assert(e == 0);
133133

134-
uv_buf_t buf = uv_buf_init(calloc(NBYTES_TO_SEND, 1), NBYTES_TO_SEND);
134+
char *data = calloc(NBYTES_TO_SEND, 1);
135+
136+
uv_buf_t buf = uv_buf_init(data, NBYTES_TO_SEND);
135137

136138
memcpy(buf.base, "hello", 5);
137139

@@ -146,6 +148,7 @@ main () {
146148
assert(nbytes_read == NBYTES_TO_SEND && read_hash == write_hash);
147149

148150
free(req);
151+
free(data);
149152

150153
return 0;
151154
}

test/stream-write-read-multiple.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,8 @@ main () {
9292

9393
assert(ack_called == 2 && total_read == buf.len * 2);
9494

95+
free(areq);
96+
free(breq);
97+
9598
return 0;
9699
}

test/stream-write-read-perf.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,24 @@ on_read (udx_stream_t *handle, ssize_t read_len, const uv_buf_t *buf) {
5959
}
6060

6161
static void
62-
on_b_sock_close () {
62+
on_b_sock_close (udx_socket_t *socket) {
6363
printf("sending socket closing\n");
6464
}
6565

6666
static void
67-
on_b_stream_close () {
67+
on_b_stream_close (udx_stream_t *stream, int status) {
6868
printf("sending stream closing\n");
6969
int e = udx_socket_close(&bsock);
7070
assert(e == 0 && "udx_socket_close (sender, 'b')");
7171
}
7272

7373
static void
74-
on_a_sock_close () {
74+
on_a_sock_close (udx_socket_t *socket) {
7575
printf("receiving socket closing\n");
7676
}
7777

7878
static void
79-
on_a_stream_close () {
79+
on_a_stream_close (udx_stream_t *stream, int status) {
8080
printf("receiving stream closing\n");
8181
int e = udx_socket_close(&asock);
8282
assert(e == 0 && "udx_socket_close (receiver, 'a')");

0 commit comments

Comments
 (0)