Skip to content

Commit fdd27e4

Browse files
authored
Merge pull request #886 from liujinhui-job/add___recv_chk_function
add __recv_chk/__read_chk/__recvfrom_chk
2 parents e284dbd + 111816e commit fdd27e4

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

adapter/syscall/ff_hook_syscall.c

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ struct ff_config ff_global_cfg;
3737

3838
#undef FF_SYSCALL_DECL
3939
#define FF_SYSCALL_DECL(ret, fn, args) strong_alias(ff_hook_##fn, fn)
40+
FF_SYSCALL_DECL(ssize_t, __recv_chk, (int, void *, size_t, size_t, int));
41+
FF_SYSCALL_DECL(ssize_t, __read_chk, (int, void *, size_t, size_t));
42+
FF_SYSCALL_DECL(ssize_t, __recvfrom_chk, (int, void *, size_t, size_t, int,
43+
struct sockaddr *, socklen_t *));
4044
#include <ff_declare_syscalls.h>
4145

4246
#define share_mem_alloc(size) rte_malloc(NULL, (size), 0)
@@ -236,6 +240,8 @@ static int ff_kernel_max_fd = FF_KERNEL_MAX_FD_DEFAULT;
236240
/* not support thread socket now */
237241
static int need_alarm_sem = 0;
238242

243+
void __chk_fail(void);
244+
239245
static inline int convert_fstack_fd(int sockfd) {
240246
return sockfd + ff_kernel_max_fd;
241247
}
@@ -788,6 +794,18 @@ ff_hook_recv(int fd, void *buf, size_t len, int flags)
788794
return ff_hook_recvfrom(fd, buf, len, flags, NULL, NULL);
789795
}
790796

797+
ssize_t
798+
ff_hook___recv_chk(int fd, void *buf, size_t buflen, size_t len, int flags)
799+
{
800+
DEBUG_LOG("ff_hook___recv_chk, fd:%d, buf:%p, len:%lu, flags:%d\n",
801+
fd, buf, len, flags);
802+
803+
if (buflen < len)
804+
__chk_fail();
805+
806+
return ff_hook_recvfrom(fd, buf, len, flags, NULL, NULL);
807+
}
808+
791809
ssize_t
792810
ff_hook_recvfrom(int fd, void *buf, size_t len, int flags,
793811
struct sockaddr *from, socklen_t *fromlen)
@@ -885,6 +903,106 @@ ff_hook_recvfrom(int fd, void *buf, size_t len, int flags,
885903
RETURN_NOFREE();
886904
}
887905

906+
ssize_t
907+
ff_hook___recvfrom_chk(int fd, void *buf, size_t len, size_t buflen, int flags,
908+
struct sockaddr *from, socklen_t *fromlen)
909+
{
910+
DEBUG_LOG("ff_hook___recvfrom_chk, fd:%d, buf:%p, len:%lu, flags:%d, from:%p, fromlen:%p\n",
911+
fd, buf, len, flags, from, fromlen);
912+
913+
if (buflen < len)
914+
__chk_fail();
915+
916+
if (buf == NULL || len == 0) {
917+
errno = EINVAL;
918+
return -1;
919+
}
920+
921+
if ((from == NULL && fromlen != NULL) ||
922+
(from != NULL && fromlen == NULL)) {
923+
errno = EINVAL;
924+
return -1;
925+
}
926+
927+
CHECK_FD_OWNERSHIP(recvfrom, (fd, buf, len, flags, from, fromlen));
928+
929+
DEFINE_REQ_ARGS_STATIC(recvfrom);
930+
static __thread void *sh_buf = NULL;
931+
static __thread size_t sh_buf_len = 0;
932+
static __thread struct sockaddr *sh_from = NULL;
933+
static __thread socklen_t sh_from_len = 0;
934+
static __thread socklen_t *sh_fromlen = NULL;
935+
936+
if (from != NULL) {
937+
if (sh_from == NULL || sh_from_len < *fromlen) {
938+
if (sh_from) {
939+
share_mem_free(sh_from);
940+
}
941+
942+
sh_from_len = *fromlen;
943+
sh_from = share_mem_alloc(sh_from_len);
944+
if (sh_from == NULL) {
945+
RETURN_ERROR_NOFREE(ENOMEM);
946+
}
947+
}
948+
949+
if (sh_fromlen == NULL) {
950+
sh_fromlen = share_mem_alloc(sizeof(socklen_t));
951+
if (sh_fromlen == NULL) {
952+
//share_mem_free(sh_from);
953+
RETURN_ERROR_NOFREE(ENOMEM);
954+
}
955+
}
956+
957+
/* sh_fromlen is input and output param */
958+
*sh_fromlen = *fromlen;
959+
960+
args->from = sh_from;
961+
args->fromlen = sh_fromlen;
962+
} else {
963+
args->from = NULL;
964+
args->fromlen = NULL;
965+
}
966+
967+
if (sh_buf == NULL || sh_buf_len < (len * 4)) {
968+
if (sh_buf) {
969+
share_mem_free(sh_buf);
970+
}
971+
972+
sh_buf_len = len * 4;
973+
sh_buf = share_mem_alloc(sh_buf_len);
974+
if (sh_buf == NULL) {
975+
RETURN_ERROR_NOFREE(ENOMEM);
976+
}
977+
}
978+
979+
args->fd = fd;
980+
args->buf = sh_buf;
981+
args->len = len;
982+
args->flags = flags;
983+
984+
SYSCALL(FF_SO_RECVFROM, args);
985+
986+
if (ret >= 0) {
987+
rte_memcpy(buf, sh_buf, ret);
988+
if (from) {
989+
socklen_t cplen = *sh_fromlen > *fromlen ? *fromlen
990+
: *sh_fromlen;
991+
rte_memcpy(from, sh_from, cplen);
992+
*fromlen = *sh_fromlen;
993+
}
994+
}
995+
996+
/*if (from) {
997+
share_mem_free(sh_from);
998+
share_mem_free(sh_fromlen);
999+
}
1000+
1001+
share_mem_free(sh_buf);*/
1002+
1003+
RETURN_NOFREE();
1004+
}
1005+
8881006
static void
8891007
iovec_share_free(struct iovec *iov, int iovcnt)
8901008
{
@@ -1307,6 +1425,54 @@ ff_hook_read(int fd, void *buf, size_t len)
13071425
RETURN_NOFREE();
13081426
}
13091427

1428+
ssize_t
1429+
ff_hook___read_chk(int fd, void *buf, size_t nbytes, size_t len)
1430+
{
1431+
DEBUG_LOG("ff_hook___read_chk, fd:%d, buf:%p, len:%lu\n", fd, buf, len);
1432+
1433+
if (buflen < nbytes)
1434+
__chk_fail();
1435+
1436+
if (buf == NULL || len == 0) {
1437+
errno = EINVAL;
1438+
return -1;
1439+
}
1440+
1441+
CHECK_FD_OWNERSHIP(read, (fd, buf, len));
1442+
1443+
DEFINE_REQ_ARGS_STATIC(read);
1444+
static __thread void *sh_buf = NULL;
1445+
static __thread size_t sh_buf_len = 0;
1446+
1447+
/* alloc or realloc sh_buf */
1448+
if (sh_buf == NULL || sh_buf_len < (len * 4)) {
1449+
if (sh_buf) {
1450+
share_mem_free(sh_buf);;
1451+
}
1452+
1453+
/* alloc 4 times buf space */
1454+
sh_buf_len = len * 4;
1455+
sh_buf = share_mem_alloc(sh_buf_len);
1456+
if (sh_buf == NULL) {
1457+
RETURN_ERROR_NOFREE(ENOMEM);
1458+
}
1459+
}
1460+
1461+
args->fd = fd;
1462+
args->buf = sh_buf;
1463+
args->len = len;
1464+
1465+
SYSCALL(FF_SO_READ, args);
1466+
1467+
if (ret > 0) {
1468+
rte_memcpy(buf, sh_buf, ret);
1469+
}
1470+
1471+
//share_mem_free(sh_buf);
1472+
1473+
RETURN_NOFREE();
1474+
}
1475+
13101476
ssize_t
13111477
ff_hook_readv(int fd, const struct iovec *iov, int iovcnt)
13121478
{

adapter/syscall/ff_hook_syscall.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
#undef FF_SYSCALL_DECL
55
#define FF_SYSCALL_DECL(ret, fn, args) extern ret ff_hook_##fn args
6+
FF_SYSCALL_DECL(ssize_t, __recv_chk, (int, void *, size_t, size_t, int));
7+
FF_SYSCALL_DECL(ssize_t, __read_chk, (int, void *, size_t, size_t));
8+
FF_SYSCALL_DECL(ssize_t, __recvfrom_chk, (int, void *, size_t, size_t, int,
9+
struct sockaddr *, socklen_t *));
610
#include <ff_declare_syscalls.h>
711

812
extern int kqueue(void);

0 commit comments

Comments
 (0)