zlib: zlib can only process 4GB at a time

The size of objects we read from the repository and data we try to put into the repository are represented in "unsigned long", so that on larger architectures we can handle objects that weigh more than 4GB. But the interface defined in zlib.h to communicate with inflate/deflate limits avail_in (how many bytes of input are we calling zlib with) and avail_out (how many bytes of output from zlib are we ready to accept) fields effectively to 4GB by defining their type to be uInt. In many places in our code, we allocate a large buffer (e.g. mmap'ing a large loose object file) and tell zlib its size by assigning the size to avail_in field of the stream, but that will truncate the high octets of the real size. The worst part of this story is that we often pass around z_stream (the state object used by zlib) to keep track of the number of used bytes in input/output buffer by inspecting these two fields, which practically limits our callchain to the same 4GB limit. Wrap z_stream in another structure git_zstream that can express avail_in and avail_out in unsigned long. For now, just die() when the caller gives a size that cannot be given to a single zlib call. In later patches in the series, we would make git_inflate() and git_deflate() internally loop to give callers an illusion that our "improved" version of zlib interface can operate on a buffer larger than 4GB in one go. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 10, 2011 at 11:52 UTC ef49a7a0126d64359c974b4b3b71d7ad42ee3bca
15 files changed +142 -70
archive-zip.c
+1 -1
@@ -90,7 +90,7 @@ static void copy_le32(unsigned char *dest, unsigned int n)
90 static void *zlib_deflate(void *data, unsigned long size,
91 int compression_level, unsigned long *compressed_size)
92 {
93 - z_stream stream;
93 + git_zstream stream;
94 unsigned long maxsize;
95 void *buffer;
96 int result;
builtin/apply.c
+1 -1
@@ -1634,7 +1634,7 @@ static inline int metadata_changes(struct patch *patch)
1634 static char *inflate_it(const void *data, unsigned long size,
1635 unsigned long inflated_size)
1636 {
1637 - z_stream stream;
1637 + git_zstream stream;
1638 void *out;
1639 int st;
1640
builtin/index-pack.c
+3 -3
@@ -265,7 +265,7 @@ static void unlink_base_data(struct base_data *c)
265 static void *unpack_entry_data(unsigned long offset, unsigned long size)
266 {
267 int status;
268 - z_stream stream;
268 + git_zstream stream;
269 void *buf = xmalloc(size);
270
271 memset(&stream, 0, sizeof(stream));
@@ -355,7 +355,7 @@ static void *get_data_from_pack(struct object_entry *obj)
355 off_t from = obj[0].idx.offset + obj[0].hdr_size;
356 unsigned long len = obj[1].idx.offset - from;
357 unsigned char *data, *inbuf;
358 - z_stream stream;
358 + git_zstream stream;
359 int status;
360
361 data = xmalloc(obj->size);
@@ -666,7 +666,7 @@ static void parse_pack_objects(unsigned char *sha1)
666
667 static int write_compressed(struct sha1file *f, void *in, unsigned int size)
668 {
669 - z_stream stream;
669 + git_zstream stream;
670 int status;
671 unsigned char outbuf[4096];
672
builtin/pack-objects.c
+5 -5
@@ -126,7 +126,7 @@ static void *get_delta(struct object_entry *entry)
126
127 static unsigned long do_compress(void **pptr, unsigned long size)
128 {
129 - z_stream stream;
129 + git_zstream stream;
130 void *in, *out;
131 unsigned long maxsize;
132
@@ -160,7 +160,7 @@ static int check_pack_inflate(struct packed_git *p,
160 off_t len,
161 unsigned long expect)
162 {
163 - z_stream stream;
163 + git_zstream stream;
164 unsigned char fakebuf[4096], *in;
165 int st;
166
@@ -187,12 +187,12 @@ static void copy_pack_data(struct sha1file *f,
187 off_t len)
188 {
189 unsigned char *in;
190 - unsigned int avail;
190 + unsigned long avail;
191
192 while (len) {
193 in = use_pack(p, w_curs, offset, &avail);
194 if (avail > len)
195 - avail = (unsigned int)len;
195 + avail = (unsigned long)len;
196 sha1write(f, in, avail);
197 offset += avail;
198 len -= avail;
@@ -994,7 +994,7 @@ static void check_object(struct object_entry *entry)
994 const unsigned char *base_ref = NULL;
995 struct object_entry *base_entry;
996 unsigned long used, used_0;
997 - unsigned int avail;
997 + unsigned long avail;
998 off_t ofs;
999 unsigned char *buf, c;
1000
builtin/unpack-objects.c
+1 -1
@@ -90,7 +90,7 @@ static void use(int bytes)
90
91 static void *get_data(unsigned long size)
92 {
93 - z_stream stream;
93 + git_zstream stream;
94 void *buf = xmalloc(size);
95
96 memset(&stream, 0, sizeof(stream));
cache.h
+22 -13
@@ -16,18 +16,27 @@
16 #endif
17
18 #include <zlib.h>
19 -
20 -void git_inflate_init(z_streamp strm);
21 -void git_inflate_init_gzip_only(z_streamp strm);
22 -void git_inflate_end(z_streamp strm);
23 -int git_inflate(z_streamp strm, int flush);
24 -
25 -void git_deflate_init(z_streamp strm, int level);
26 -void git_deflate_init_gzip(z_streamp strm, int level);
27 -void git_deflate_end(z_streamp strm);
28 -int git_deflate_end_gently(z_streamp strm);
29 -int git_deflate(z_streamp strm, int flush);
30 -unsigned long git_deflate_bound(z_streamp, unsigned long);
19 +typedef struct git_zstream {
20 + z_stream z;
21 + unsigned long avail_in;
22 + unsigned long avail_out;
23 + unsigned long total_in;
24 + unsigned long total_out;
25 + unsigned char *next_in;
26 + unsigned char *next_out;
27 +} git_zstream;
28 +
29 +void git_inflate_init(git_zstream *);
30 +void git_inflate_init_gzip_only(git_zstream *);
31 +void git_inflate_end(git_zstream *);
32 +int git_inflate(git_zstream *, int flush);
33 +
34 +void git_deflate_init(git_zstream *, int level);
35 +void git_deflate_init_gzip(git_zstream *, int level);
36 +void git_deflate_end(git_zstream *);
37 +int git_deflate_end_gently(git_zstream *);
38 +int git_deflate(git_zstream *, int flush);
39 +unsigned long git_deflate_bound(git_zstream *, unsigned long);
40
41 #if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
42 #define DTYPE(de) ((de)->d_type)
@@ -991,7 +1000,7 @@ extern struct packed_git *find_sha1_pack(const unsigned char *sha1,
1000 extern void pack_report(void);
1001 extern int open_pack_index(struct packed_git *);
1002 extern void close_pack_index(struct packed_git *);
994 -extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned int *);
1003 +extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned long *);
1004 extern void close_pack_windows(struct packed_git *);
1005 extern void unuse_pack(struct pack_window **);
1006 extern void free_pack_by_name(const char *);
diff.c
+1 -1
@@ -1729,7 +1729,7 @@ static unsigned char *deflate_it(char *data,
1729 {
1730 int bound;
1731 unsigned char *deflated;
1732 - z_stream stream;
1732 + git_zstream stream;
1733
1734 memset(&stream, 0, sizeof(stream));
1735 git_deflate_init(&stream, zlib_compression_level);
fast-import.c
+2 -2
@@ -1017,7 +1017,7 @@ static int store_object(
1017 unsigned char sha1[20];
1018 unsigned long hdrlen, deltalen;
1019 git_SHA_CTX c;
1020 - z_stream s;
1020 + git_zstream s;
1021
1022 hdrlen = sprintf((char *)hdr,"%s %lu", typename(type),
1023 (unsigned long)dat->len) + 1;
@@ -1163,7 +1163,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
1163 off_t offset;
1164 git_SHA_CTX c;
1165 git_SHA_CTX pack_file_ctx;
1166 - z_stream s;
1166 + git_zstream s;
1167 int status = Z_OK;
1168
1169 /* Determine if we should auto-checkpoint. */
http-backend.c
+1 -1
@@ -271,7 +271,7 @@ static struct rpc_service *select_service(const char *name)
271
272 static void inflate_request(const char *prog_name, int out)
273 {
274 - z_stream stream;
274 + git_zstream stream;
275 unsigned char in_buf[8192];
276 unsigned char out_buf[8192];
277 unsigned long cnt = 0;
http-push.c
+1 -1
@@ -352,7 +352,7 @@ static void start_put(struct transfer_request *request)
352 unsigned long len;
353 int hdrlen;
354 ssize_t size;
355 - z_stream stream;
355 + git_zstream stream;
356
357 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
358 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
http.h
+1 -1
@@ -172,7 +172,7 @@ struct http_object_request {
172 unsigned char sha1[20];
173 unsigned char real_sha1[20];
174 git_SHA_CTX c;
175 - z_stream stream;
175 + git_zstream stream;
176 int zret;
177 int rename;
178 struct active_request_slot *slot;
pack-check.c
+2 -2
@@ -26,7 +26,7 @@ int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
26 uint32_t data_crc = crc32(0, NULL, 0);
27
28 do {
29 - unsigned int avail;
29 + unsigned long avail;
30 void *data = use_pack(p, w_curs, offset, &avail);
31 if (avail > len)
32 avail = len;
@@ -61,7 +61,7 @@ static int verify_packfile(struct packed_git *p,
61
62 git_SHA1_Init(&ctx);
63 do {
64 - unsigned int remaining;
64 + unsigned long remaining;
65 unsigned char *in = use_pack(p, w_curs, offset, &remaining);
66 offset += remaining;
67 if (!pack_sig_ofs)
remote-curl.c
+1 -1
@@ -471,7 +471,7 @@ static int post_rpc(struct rpc_state *rpc)
471 * the transfer time.
472 */
473 size_t size;
474 - z_stream stream;
474 + git_zstream stream;
475 int ret;
476
477 memset(&stream, 0, sizeof(stream));
sha1_file.c
+9 -9
@@ -833,7 +833,7 @@ static int in_window(struct pack_window *win, off_t offset)
833 unsigned char *use_pack(struct packed_git *p,
834 struct pack_window **w_cursor,
835 off_t offset,
836 - unsigned int *left)
836 + unsigned long *left)
837 {
838 struct pack_window *win = *w_cursor;
839
@@ -1244,7 +1244,7 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
1244 return used;
1245 }
1246
1247 -static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
1247 +static int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
1248 {
1249 unsigned long size, used;
1250 static const char valid_loose_object_type[8] = {
@@ -1291,7 +1291,7 @@ static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned lon
1291 return 0;
1292 }
1293
1294 -static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
1294 +static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
1295 {
1296 int bytes = strlen(buffer) + 1;
1297 unsigned char *buf = xmallocz(size);
@@ -1390,7 +1390,7 @@ static int parse_sha1_header(const char *hdr, unsigned long *sizep)
1390 static void *unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size, const unsigned char *sha1)
1391 {
1392 int ret;
1393 - z_stream stream;
1393 + git_zstream stream;
1394 char hdr[8192];
1395
1396 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
@@ -1406,7 +1406,7 @@ unsigned long get_size_from_delta(struct packed_git *p,
1406 {
1407 const unsigned char *data;
1408 unsigned char delta_head[20], *in;
1409 - z_stream stream;
1409 + git_zstream stream;
1410 int st;
1411
1412 memset(&stream, 0, sizeof(stream));
@@ -1528,7 +1528,7 @@ static int unpack_object_header(struct packed_git *p,
1528 unsigned long *sizep)
1529 {
1530 unsigned char *base;
1531 - unsigned int left;
1531 + unsigned long left;
1532 unsigned long used;
1533 enum object_type type;
1534
@@ -1641,7 +1641,7 @@ static void *unpack_compressed_entry(struct packed_git *p,
1641 unsigned long size)
1642 {
1643 int st;
1644 - z_stream stream;
1644 + git_zstream stream;
1645 unsigned char *buffer, *in;
1646
1647 buffer = xmallocz(size);
@@ -2074,7 +2074,7 @@ static int sha1_loose_object_info(const unsigned char *sha1, unsigned long *size
2074 int status;
2075 unsigned long mapsize, size;
2076 void *map;
2077 - z_stream stream;
2077 + git_zstream stream;
2078 char hdr[32];
2079
2080 map = map_sha1_file(sha1, &mapsize);
@@ -2425,7 +2425,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
2425 {
2426 int fd, ret;
2427 unsigned char compressed[4096];
2428 - z_stream stream;
2428 + git_zstream stream;
2429 git_SHA_CTX c;
2430 unsigned char parano_sha1[20];
2431 char *filename;
zlib.c
+91 -28
@@ -22,45 +22,90 @@ static const char *zerr_to_string(int status)
22 }
23 }
24
25 -void git_inflate_init(z_streamp strm)
25 +/*
26 + * avail_in and avail_out in zlib are counted in uInt, which typically
27 + * limits the size of the buffer we can use to 4GB when interacting
28 + * with zlib in a single call to inflate/deflate.
29 + */
30 +#define ZLIB_BUF_MAX ((uInt)-1)
31 +static inline uInt zlib_buf_cap(unsigned long len)
32 +{
33 + if (ZLIB_BUF_MAX < len)
34 + die("working buffer for zlib too large");
35 + return len;
36 +}
37 +
38 +static void zlib_pre_call(git_zstream *s)
39 +{
40 + s->z.next_in = s->next_in;
41 + s->z.next_out = s->next_out;
42 + s->z.total_in = s->total_in;
43 + s->z.total_out = s->total_out;
44 + s->z.avail_in = zlib_buf_cap(s->avail_in);
45 + s->z.avail_out = zlib_buf_cap(s->avail_out);
46 +}
47 +
48 +static void zlib_post_call(git_zstream *s)
49 +{
50 + s->next_in = s->z.next_in;
51 + s->next_out = s->z.next_out;
52 + s->total_in = s->z.total_in;
53 + s->total_out = s->z.total_out;
54 + s->avail_in = s->z.avail_in;
55 + s->avail_out = s->z.avail_out;
56 +}
57 +
58 +void git_inflate_init(git_zstream *strm)
59 {
27 - int status = inflateInit(strm);
60 + int status;
61
62 + zlib_pre_call(strm);
63 + status = inflateInit(&strm->z);
64 + zlib_post_call(strm);
65 if (status == Z_OK)
66 return;
67 die("inflateInit: %s (%s)", zerr_to_string(status),
32 - strm->msg ? strm->msg : "no message");
68 + strm->z.msg ? strm->z.msg : "no message");
69 }
70
35 -void git_inflate_init_gzip_only(z_streamp strm)
71 +void git_inflate_init_gzip_only(git_zstream *strm)
72 {
73 /*
74 * Use default 15 bits, +16 is to accept only gzip and to
75 * yield Z_DATA_ERROR when fed zlib format.
76 */
77 const int windowBits = 15 + 16;
42 - int status = inflateInit2(strm, windowBits);
78 + int status;
79
80 + zlib_pre_call(strm);
81 + status = inflateInit2(&strm->z, windowBits);
82 + zlib_post_call(strm);
83 if (status == Z_OK)
84 return;
85 die("inflateInit2: %s (%s)", zerr_to_string(status),
47 - strm->msg ? strm->msg : "no message");
86 + strm->z.msg ? strm->z.msg : "no message");
87 }
88
50 -void git_inflate_end(z_streamp strm)
89 +void git_inflate_end(git_zstream *strm)
90 {
52 - int status = inflateEnd(strm);
91 + int status;
92
93 + zlib_pre_call(strm);
94 + status = inflateEnd(&strm->z);
95 + zlib_post_call(strm);
96 if (status == Z_OK)
97 return;
98 error("inflateEnd: %s (%s)", zerr_to_string(status),
57 - strm->msg ? strm->msg : "no message");
99 + strm->z.msg ? strm->z.msg : "no message");
100 }
101
60 -int git_inflate(z_streamp strm, int flush)
102 +int git_inflate(git_zstream *strm, int flush)
103 {
62 - int status = inflate(strm, flush);
104 + int status;
105
106 + zlib_pre_call(strm);
107 + status = inflate(&strm->z, flush);
108 + zlib_post_call(strm);
109 switch (status) {
110 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
111 case Z_BUF_ERROR:
@@ -74,7 +119,7 @@ int git_inflate(z_streamp strm, int flush)
119 break;
120 }
121 error("inflate: %s (%s)", zerr_to_string(status),
77 - strm->msg ? strm->msg : "no message");
122 + strm->z.msg ? strm->z.msg : "no message");
123 return status;
124 }
125
@@ -82,56 +127,74 @@ int git_inflate(z_streamp strm, int flush)
127 #define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
128 #endif
129
85 -unsigned long git_deflate_bound(z_streamp strm, unsigned long size)
130 +unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
131 {
87 - return deflateBound(strm, size);
132 + return deflateBound(&strm->z, size);
133 }
134
90 -void git_deflate_init(z_streamp strm, int level)
135 +void git_deflate_init(git_zstream *strm, int level)
136 {
92 - int status = deflateInit(strm, level);
137 + int status;
138
139 + zlib_pre_call(strm);
140 + status = deflateInit(&strm->z, level);
141 + zlib_post_call(strm);
142 if (status == Z_OK)
143 return;
144 die("deflateInit: %s (%s)", zerr_to_string(status),
97 - strm->msg ? strm->msg : "no message");
145 + strm->z.msg ? strm->z.msg : "no message");
146 }
147
100 -void git_deflate_init_gzip(z_streamp strm, int level)
148 +void git_deflate_init_gzip(git_zstream *strm, int level)
149 {
150 /*
151 * Use default 15 bits, +16 is to generate gzip header/trailer
152 * instead of the zlib wrapper.
153 */
154 const int windowBits = 15 + 16;
107 - int status = deflateInit2(strm, level,
155 + int status;
156 +
157 + zlib_pre_call(strm);
158 + status = deflateInit2(&strm->z, level,
159 Z_DEFLATED, windowBits,
160 8, Z_DEFAULT_STRATEGY);
161 + zlib_post_call(strm);
162 if (status == Z_OK)
163 return;
164 die("deflateInit2: %s (%s)", zerr_to_string(status),
113 - strm->msg ? strm->msg : "no message");
165 + strm->z.msg ? strm->z.msg : "no message");
166 }
167
116 -void git_deflate_end(z_streamp strm)
168 +void git_deflate_end(git_zstream *strm)
169 {
118 - int status = deflateEnd(strm);
170 + int status;
171
172 + zlib_pre_call(strm);
173 + status = deflateEnd(&strm->z);
174 + zlib_post_call(strm);
175 if (status == Z_OK)
176 return;
177 error("deflateEnd: %s (%s)", zerr_to_string(status),
123 - strm->msg ? strm->msg : "no message");
178 + strm->z.msg ? strm->z.msg : "no message");
179 }
180
126 -int git_deflate_end_gently(z_streamp strm)
181 +int git_deflate_end_gently(git_zstream *strm)
182 {
128 - return deflateEnd(strm);
183 + int status;
184 +
185 + zlib_pre_call(strm);
186 + status = deflateEnd(&strm->z);
187 + zlib_post_call(strm);
188 + return status;
189 }
190
131 -int git_deflate(z_streamp strm, int flush)
191 +int git_deflate(git_zstream *strm, int flush)
192 {
133 - int status = deflate(strm, flush);
193 + int status;
194
195 + zlib_pre_call(strm);
196 + status = deflate(&strm->z, flush);
197 + zlib_post_call(strm);
198 switch (status) {
199 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
200 case Z_BUF_ERROR:
@@ -145,6 +208,6 @@ int git_deflate(z_streamp strm, int flush)
208 break;
209 }
210 error("deflate: %s (%s)", zerr_to_string(status),
148 - strm->msg ? strm->msg : "no message");
211 + strm->z.msg ? strm->z.msg : "no message");
212 return status;
213 }