1 // Test that ASan detects buffer overflow on read from socket via recvfrom.
3 // RUN: %clangxx_asan %s -DRECVFROM -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-RECVFROM
4 // RUN: %clangxx_asan %s -DSENDTO -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SENDTO
5 // RUN: %clangxx_asan %s -DSENDTO -o %t && %env_asan_opts=intercept_send=0 %run %t 2>&1
7 // This will try to fast unwind on Arm Thumb, where fast unwinding does not work.
8 // UNSUPPORTED: android, !fast-unwinder-works
15 #include <sys/types.h>
16 #include <sys/socket.h>
19 #define CHECK_ERROR(p, m) \
22 fprintf(stderr, "ERROR " m "\n"); \
27 const int kBufSize
= 10;
30 static void *client_thread_udp(void *data
) {
32 const char buf
[kBufSize
/ 2] = {0, };
34 const char buf
[kBufSize
] = {0, };
36 struct sockaddr_in serveraddr
;
37 socklen_t addrlen
= sizeof(serveraddr
);
39 int succeeded
= getsockname(sockfd
, (struct sockaddr
*)&serveraddr
, &addrlen
);
40 CHECK_ERROR(succeeded
< 0, "in getsockname");
42 succeeded
= sendto(sockfd
, buf
, kBufSize
, 0, (struct sockaddr
*)&serveraddr
,
44 // CHECK-SENDTO: {{READ of size 10 at 0x.* thread T1}}
45 // CHECK-SENDTO: {{ #1 0x.* in client_thread_udp.*recvfrom.cpp:}}[[@LINE-3]]
46 CHECK_ERROR(succeeded
< 0, "in sending message");
52 char buf
[kBufSize
/ 2];
56 pthread_t client_thread
;
57 struct sockaddr_in serveraddr
;
59 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
60 CHECK_ERROR(sockfd
< 0, "opening socket");
62 memset(&serveraddr
, 0, sizeof(serveraddr
));
63 serveraddr
.sin_family
= AF_INET
;
64 serveraddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
65 serveraddr
.sin_port
= 0;
67 int bound
= bind(sockfd
, (struct sockaddr
*)&serveraddr
, sizeof(serveraddr
));
68 CHECK_ERROR(bound
< 0, "on binding");
71 pthread_create(&client_thread
, NULL
, client_thread_udp
, &serveraddr
);
72 CHECK_ERROR(succeeded
, "creating thread");
74 recvfrom(sockfd
, buf
, kBufSize
, 0, NULL
, NULL
); // BOOM
75 // CHECK-RECVFROM: {{WRITE of size 10 at 0x.* thread T0}}
76 // CHECK-RECVFROM: {{ #1 0x.* in main.*recvfrom.cpp:}}[[@LINE-2]]
77 // CHECK-RECVFROM: {{Address 0x.* is located in stack of thread T0 at offset}}
78 // CHECK-RECVFROM-NEXT: in{{.*}}main{{.*}}recvfrom.cpp
79 succeeded
= pthread_join(client_thread
, NULL
);
80 CHECK_ERROR(succeeded
, "joining thread");