Skip to content

Commit 8229bce

Browse files
committed
ibuf: rename ibuf_*() to memcached_ibuf_*()
See [1] for the problem root and [2] for analysis. In brief: `ibuf_*()` symbols may clash with the same named symbols in tarantool, so it worth to rename them to avoid effects of different binary layouts: assertion fails and memory corruptions. For example, `ibuf_reserve_slow()` calls `slab_get()` and `slab_put()`. If something will be changed in tarantool's slab cache, we can meet problems in the module. This patch just renames the `ibuf_*()` functions. It is the most simple and durable workaround of the problem. It is likely not necessary to rename inline functions, but since I'm not dead sure, I renamed them all. It is the last known problem from #59. All others are confirmed as safe to ignore if we'll keep current libsmall version in the module (see the analysis in [2]). [1]: tarantool/tarantool#6873 [2]: #59 (comment) Fixes #59
1 parent e143578 commit 8229bce

File tree

4 files changed

+46
-42
lines changed

4 files changed

+46
-42
lines changed

memcached/internal/memcached.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
static inline int
5050
memcached_skip_request(struct memcached_connection *con) {
5151
struct ibuf *in = con->in;
52-
while (ibuf_used(in) < con->len && con->noprocess) {
53-
con->len -= ibuf_used(in);
54-
ibuf_reset(in);
52+
while (memcached_ibuf_used(in) < con->len && con->noprocess) {
53+
con->len -= memcached_ibuf_used(in);
54+
memcached_ibuf_reset(in);
5555
ssize_t read = mnet_read_ibuf(con->fd, in, 1);
5656
if (read == -1)
5757
memcached_error_ENOMEM(1, "ibuf");
@@ -70,10 +70,10 @@ memcached_flush(struct memcached_connection *con) {
7070
obuf_iovcnt(con->out),
7171
obuf_size(con->out));
7272
con->cfg->stat.bytes_written += total;
73-
if (ibuf_used(con->in) == 0)
74-
ibuf_reset(con->in);
73+
if (memcached_ibuf_used(con->in) == 0)
74+
memcached_ibuf_reset(con->in);
7575
obuf_reset(con->out);
76-
if (ibuf_reserve(con->in, con->cfg->readahead) == NULL)
76+
if (memcached_ibuf_reserve(con->in, con->cfg->readahead) == NULL)
7777
return -1;
7878
return total;
7979
}
@@ -92,7 +92,7 @@ memcached_connection_gc(struct memcached_connection *con)
9292
static inline int
9393
memcached_loop_read(struct memcached_connection *con, size_t to_read)
9494
{
95-
if (ibuf_reserve(con->in, to_read) == NULL) {
95+
if (memcached_ibuf_reserve(con->in, to_read) == NULL) {
9696
/* memcached_error_ENOMEM(to_read, "ibuf");*/
9797
return -1;
9898
}
@@ -198,7 +198,7 @@ memcached_loop(struct memcached_connection *con)
198198
if (con->close_connection) {
199199
say_debug("Requesting exit. Exiting.");
200200
break;
201-
} else if (rc == 0 && ibuf_used(con->in) > 0 &&
201+
} else if (rc == 0 && memcached_ibuf_used(con->in) > 0 &&
202202
batch_count < con->cfg->batch_count) {
203203
batch_count++;
204204
goto next;
@@ -208,7 +208,7 @@ memcached_loop(struct memcached_connection *con)
208208
memcached_flush(con);
209209
fiber_reschedule();
210210
batch_count = 0;
211-
if (ibuf_used(con->in) > 0) {
211+
if (memcached_ibuf_used(con->in) > 0) {
212212
goto next;
213213
}
214214
continue;

memcached/internal/network.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ibuf_new()
4848
{
4949
void *ibuf = mempool_alloc(&ibuf_pool);
5050
if (ibuf == NULL) return NULL;
51-
ibuf_create((struct ibuf *)ibuf, cord_slab_cache(), iobuf_readahead);
51+
memcached_ibuf_create((struct ibuf *)ibuf, cord_slab_cache(), iobuf_readahead);
5252
return ibuf;
5353
}
5454

@@ -64,7 +64,7 @@ obuf_new()
6464
void
6565
iobuf_delete(struct ibuf *ibuf, struct obuf *obuf)
6666
{
67-
ibuf_destroy(ibuf);
67+
memcached_ibuf_destroy(ibuf);
6868
obuf_destroy(obuf);
6969
mempool_free(&ibuf_pool, ibuf);
7070
mempool_free(&obuf_pool, obuf);
@@ -147,10 +147,11 @@ mnet_read_ahead(int fd, void *buf, size_t bufsz, size_t sz)
147147
size_t
148148
mnet_read_ibuf(int fd, struct ibuf *buf, size_t sz)
149149
{
150-
if (ibuf_reserve(buf, sz) == NULL) {
150+
if (memcached_ibuf_reserve(buf, sz) == NULL) {
151151
return -1;
152152
}
153-
ssize_t n = mnet_read_ahead(fd, buf->wpos, ibuf_unused(buf), sz);
153+
ssize_t n = mnet_read_ahead(fd, buf->wpos, memcached_ibuf_unused(buf),
154+
sz);
154155
buf->wpos += n;
155156
return n;
156157
}

third_party/memcached_ibuf.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434

3535
/** Initialize an input buffer. */
3636
void
37-
ibuf_create(struct ibuf *ibuf, struct slab_cache *slabc, size_t start_capacity)
37+
memcached_ibuf_create(struct ibuf *ibuf, struct slab_cache *slabc,
38+
size_t start_capacity)
3839
{
3940
ibuf->slabc = slabc;
4041
ibuf->buf = ibuf->rpos = ibuf->wpos = ibuf->end = NULL;
@@ -43,7 +44,7 @@ ibuf_create(struct ibuf *ibuf, struct slab_cache *slabc, size_t start_capacity)
4344
}
4445

4546
void
46-
ibuf_destroy(struct ibuf *ibuf)
47+
memcached_ibuf_destroy(struct ibuf *ibuf)
4748
{
4849
if (ibuf->buf) {
4950
struct slab *slab = slab_from_data(ibuf->buf);
@@ -53,12 +54,12 @@ ibuf_destroy(struct ibuf *ibuf)
5354

5455
/** Free memory allocated by this buffer */
5556
void
56-
ibuf_reinit(struct ibuf *ibuf)
57+
memcached_ibuf_reinit(struct ibuf *ibuf)
5758
{
5859
struct slab_cache *slabc = ibuf->slabc;
5960
size_t start_capacity = ibuf->start_capacity;
60-
ibuf_destroy(ibuf);
61-
ibuf_create(ibuf, slabc, start_capacity);
61+
memcached_ibuf_destroy(ibuf);
62+
memcached_ibuf_create(ibuf, slabc, start_capacity);
6263
}
6364

6465
/**
@@ -67,11 +68,11 @@ ibuf_reinit(struct ibuf *ibuf)
6768
* the beginning.
6869
*/
6970
void *
70-
ibuf_reserve_slow(struct ibuf *ibuf, size_t size)
71+
memcached_ibuf_reserve_slow(struct ibuf *ibuf, size_t size)
7172
{
7273
assert(ibuf->wpos + size > ibuf->end);
73-
size_t used = ibuf_used(ibuf);
74-
size_t capacity = ibuf_capacity(ibuf);
74+
size_t used = memcached_ibuf_used(ibuf);
75+
size_t capacity = memcached_ibuf_capacity(ibuf);
7576
/*
7677
* Check if we have enough space in the
7778
* current buffer. In this case de-fragment it

third_party/memcached_ibuf.h

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef TARANTOOL_SMALL_IBUF_H_INCLUDED
2-
#define TARANTOOL_SMALL_IBUF_H_INCLUDED
1+
#ifndef MEMCACHED_IBUF_H_INCLUDED
2+
#define MEMCACHED_IBUF_H_INCLUDED
33
/*
44
* Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
55
*
@@ -69,33 +69,34 @@ struct ibuf
6969
};
7070

7171
void
72-
ibuf_create(struct ibuf *ibuf, struct slab_cache *slabc, size_t start_capacity);
72+
memcached_ibuf_create(struct ibuf *ibuf, struct slab_cache *slabc,
73+
size_t start_capacity);
7374

7475
void
75-
ibuf_destroy(struct ibuf *ibuf);
76+
memcached_ibuf_destroy(struct ibuf *ibuf);
7677

7778
void
78-
ibuf_reinit(struct ibuf *ibuf);
79+
memcached_ibuf_reinit(struct ibuf *ibuf);
7980

8081
/** How much data is read and is not parsed yet. */
8182
static inline size_t
82-
ibuf_used(struct ibuf *ibuf)
83+
memcached_ibuf_used(struct ibuf *ibuf)
8384
{
8485
assert(ibuf->wpos >= ibuf->rpos);
8586
return ibuf->wpos - ibuf->rpos;
8687
}
8788

8889
/** How much data can we fit beyond buf->wpos */
8990
static inline size_t
90-
ibuf_unused(struct ibuf *ibuf)
91+
memcached_ibuf_unused(struct ibuf *ibuf)
9192
{
9293
assert(ibuf->wpos <= ibuf->end);
9394
return ibuf->end - ibuf->wpos;
9495
}
9596

9697
/** How much memory is allocated */
9798
static inline size_t
98-
ibuf_capacity(struct ibuf *ibuf)
99+
memcached_ibuf_capacity(struct ibuf *ibuf)
99100
{
100101
return ibuf->end - ibuf->buf;
101102
}
@@ -105,38 +106,38 @@ ibuf_capacity(struct ibuf *ibuf)
105106
* in case of realloc.
106107
*/
107108
static inline size_t
108-
ibuf_pos(struct ibuf *ibuf)
109+
memcached_ibuf_pos(struct ibuf *ibuf)
109110
{
110111
assert(ibuf->buf <= ibuf->rpos);
111112
return ibuf->rpos - ibuf->buf;
112113
}
113114

114115
/** Forget all cached input. */
115116
static inline void
116-
ibuf_reset(struct ibuf *ibuf)
117+
memcached_ibuf_reset(struct ibuf *ibuf)
117118
{
118119
ibuf->rpos = ibuf->wpos = ibuf->buf;
119120
}
120121

121122
void *
122-
ibuf_reserve_slow(struct ibuf *ibuf, size_t size);
123+
memcached_ibuf_reserve_slow(struct ibuf *ibuf, size_t size);
123124

124125
static inline void *
125-
ibuf_reserve(struct ibuf *ibuf, size_t size)
126+
memcached_ibuf_reserve(struct ibuf *ibuf, size_t size)
126127
{
127128
if (ibuf->wpos + size <= ibuf->end)
128129
return ibuf->wpos;
129-
return ibuf_reserve_slow(ibuf, size);
130+
return memcached_ibuf_reserve_slow(ibuf, size);
130131
}
131132

132133
static inline void *
133-
ibuf_alloc(struct ibuf *ibuf, size_t size)
134+
memcached_ibuf_alloc(struct ibuf *ibuf, size_t size)
134135
{
135136
void *ptr;
136137
if (ibuf->wpos + size <= ibuf->end)
137138
ptr = ibuf->wpos;
138139
else {
139-
ptr = ibuf_reserve_slow(ibuf, size);
140+
ptr = memcached_ibuf_reserve_slow(ibuf, size);
140141
if (ptr == NULL)
141142
return NULL;
142143
}
@@ -145,22 +146,23 @@ ibuf_alloc(struct ibuf *ibuf, size_t size)
145146
}
146147

147148
static inline void *
148-
ibuf_reserve_cb(void *ctx, size_t *size)
149+
memcached_ibuf_reserve_cb(void *ctx, size_t *size)
149150
{
150151
struct ibuf *buf = (struct ibuf *) ctx;
151-
void *p = ibuf_reserve(buf, *size ? *size : buf->start_capacity);
152-
*size = ibuf_unused(buf);
152+
void *p = memcached_ibuf_reserve(
153+
buf, *size ? *size : buf->start_capacity);
154+
*size = memcached_ibuf_unused(buf);
153155
return p;
154156
}
155157

156158
static inline void *
157-
ibuf_alloc_cb(void *ctx, size_t size)
159+
memcached_ibuf_alloc_cb(void *ctx, size_t size)
158160
{
159-
return ibuf_alloc((struct ibuf *) ctx, size);
161+
return memcached_ibuf_alloc((struct ibuf *) ctx, size);
160162
}
161163

162164
#if defined(__cplusplus)
163165
} /* extern "C" */
164166
#endif /* defined(__cplusplus) */
165167

166-
#endif /* TARANTOOL_SMALL_IBUF_H_INCLUDED */
168+
#endif /* MEMCACHED_IBUF_H_INCLUDED */

0 commit comments

Comments
 (0)