Skip to content

Commit

Permalink
expanded print_z_stream to see internal zlib state
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdowle committed Nov 29, 2020
1 parent 8480b6a commit 5b8f25b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions R/test.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ test.data.table = function(script="tests.Rraw", verbose=FALSE, pkg=".", silent=F
", Sys.getlocale()=='", Sys.getlocale(), "'",
", l10n_info()=='", paste0(names(l10n_info()), "=", l10n_info(), collapse="; "), "'",
", getDTthreads()=='", paste0(gsub("[ ][ ]+","==",gsub("^[ ]+","",capture.output(invisible(getDTthreads(verbose=TRUE))))), collapse="; "), "'",
", ", .Call(Cdt_zlib_version),
"\n", sep="")

if (inherits(err,"try-error")) {
Expand Down
42 changes: 30 additions & 12 deletions src/fwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,16 +563,41 @@ int init_stream(z_stream *stream) {
return err; // # nocov
}

void print_z_stream(const z_stream *s) // temporary tracing function for #4099
{
const char *byte = (char *)s;
for (int i=0; i<sizeof(z_stream); ++i) {
DTPRINT("%02x ", *(unsigned char *)byte++); // not byte[i] is attempt to avoid valgrind's use-of-uninitialized, #4639, and z_stream={0} now too
}
if (sizeof(z_stream)==56 || sizeof(z_stream)==112) {
// 56 on 32bit solaris. trace z_stream->state->status which zlib:deflateStateCheck checks
// this structure is not exposed, so we'll get to it via memory offsets using the trace output we put in to show on CRAN's Solaris output
const char *pos = (char *)s + (sizeof(z_stream)==56 ? 28 : 56); // ->state just happens to be halfway in the structure.
byte = *(char **)pos;
DTPRINT("state: ");
for (int i=0; i<(sizeof(z_stream)==56 ? 8 : 12); ++i) {
DTPRINT("%02x ", *(unsigned char *)byte++); // not byte[i] is attempt to avoid valgrind's use-of-uninitialized, #4639, and z_stream={0} now too
}
DTPRINT("strm: %p state->status==%d", s, *(int *)(byte-4)); // check state->strm == strm as zlib:deflateStateCheck does
}
DTPRINT("\n");
}

int compressbuff(z_stream *stream, void* dest, size_t *destLen, const void* source, size_t sourceLen)
{
stream->next_out = dest;
stream->avail_out = *destLen;
stream->next_in = (Bytef *)source; // don't use z_const anywhere; #3939
stream->avail_in = sourceLen;
if (verbose) DTPRINT(_("deflate input stream: %p %d %p %d\n"), stream->next_out, (int)(stream->avail_out), stream->next_in, (int)(stream->avail_in));

if (verbose) {
DTPRINT(_("deflate input stream: %p %d %p %d z_stream: "), stream->next_out, (int)(stream->avail_out), stream->next_in, (int)(stream->avail_in));
print_z_stream(stream);
}
int err = deflate(stream, Z_FINISH);
if (verbose) DTPRINT(_("deflate returned %d with stream->total_out==%d; Z_FINISH==%d, Z_OK==%d, Z_STREAM_END==%d\n"), err, (int)(stream->total_out), Z_FINISH, Z_OK, Z_STREAM_END);
if (verbose) {
DTPRINT(_("deflate returned %d with stream->total_out==%d; Z_FINISH==%d, Z_OK==%d, Z_STREAM_END==%d z_stream: "), err, (int)(stream->total_out), Z_FINISH, Z_OK, Z_STREAM_END);
print_z_stream(stream);
}
if (err == Z_OK) {
// with Z_FINISH, deflate must return Z_STREAM_END if correct, otherwise it's an error and we shouldn't return Z_OK (0)
err = -9; // # nocov
Expand All @@ -581,15 +606,6 @@ int compressbuff(z_stream *stream, void* dest, size_t *destLen, const void* sour
return err == Z_STREAM_END ? Z_OK : err;
}

void print_z_stream(const z_stream *s) // temporary tracing function for #4099
{
const unsigned char *byte = (unsigned char *)s;
for (int i=0; i<sizeof(z_stream); ++i) {
DTPRINT("%02x ", *byte++); // not byte[i] is attempt to avoid valgrind's use-of-uninitialized, #4639, and z_stream={0} now too
}
DTPRINT("\n");
}

void fwriteMain(fwriteMainArgs args)
{
double startTime = wallclock();
Expand Down Expand Up @@ -986,3 +1002,5 @@ void fwriteMain(fwriteMainArgs args)
// # nocov end
}
}


2 changes: 2 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ SEXP unlock();
SEXP islockedR();
SEXP allNAR();
SEXP test_dt_win_snprintf();
SEXP dt_zlib_version();

// .Externals
SEXP fastmean();
Expand Down Expand Up @@ -217,6 +218,7 @@ R_CallMethodDef callMethods[] = {
{"CtestMsgR", (DL_FUNC) &testMsgR, -1},
{"C_allNAR", (DL_FUNC) &allNAR, -1},
{"Ctest_dt_win_snprintf", (DL_FUNC)&test_dt_win_snprintf, -1},
{"Cdt_zlib_version", (DL_FUNC)&dt_zlib_version, -1},
{NULL, NULL, 0}
};

Expand Down
7 changes: 7 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,10 @@ SEXP coerceUtf8IfNeeded(SEXP x) {
return(ans);
}

#include <zlib.h>
SEXP dt_zlib_version() {
char out[51];
snprintf(out, 50, "zlibVersion()==%s ZLIB_VERSION==%s", zlibVersion(), ZLIB_VERSION);
return ScalarString(mkChar(out));
}

0 comments on commit 5b8f25b

Please sign in to comment.