Skip to content

Commit

Permalink
lightningd: add runtime checking for all system-provided libs.
Browse files Browse the repository at this point in the history
And I tested this by rolling my own libz; make indeed detects
the change and fixes it.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Dec 14, 2018
1 parent 2a90325 commit 31a375a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,11 @@ tools/headerversions: FORCE tools/headerversions.o $(CCAN_OBJS)
gen_header_versions.h: tools/headerversions
@tools/headerversions $@

# All binaries require the external libs, ccan and external library versions.
$(ALL_PROGRAMS) $(ALL_TEST_PROGRAMS): $(EXTERNAL_LIBS) $(CCAN_OBJS) gen_header_versions.h
# Rebuild the world if this changes.
ALL_GEN_HEADERS += gen_header_versions.h

# All binaries require the external libs, ccan and system library versions.
$(ALL_PROGRAMS) $(ALL_TEST_PROGRAMS): $(EXTERNAL_LIBS) $(CCAN_OBJS)

# Each test program depends on its own object.
$(ALL_TEST_PROGRAMS): %: %.o
Expand Down
12 changes: 12 additions & 0 deletions lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

#include <errno.h>
#include <fcntl.h>
#include <gen_header_versions.h>
#include <lightningd/bitcoind.h>
#include <lightningd/chaintopology.h>
#include <lightningd/channel_control.h>
Expand Down Expand Up @@ -581,6 +582,17 @@ int main(int argc, char *argv[])

/*~ What happens in strange locales should stay there. */
setup_locale();

/*~ This checks that the system-installed libraries (usually
* dynamically linked) actually are compatible with the ones we
* compiled with.
*
* The header itself is auto-generated every time the version of the
* installed libraries changes, as we had an sqlite3 version update
* which broke people, and "make" didn't think there was any work to
* do, so rebuilding didn't fix it. */
check_linked_library_versions();

/*~ Every daemon calls this in some form: the hooks are for dumping
* backtraces when we crash (if supported on this platform). */
daemon_setup(argv[0], log_backtrace_print, log_backtrace_exit);
Expand Down
38 changes: 33 additions & 5 deletions tools/headerversions.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,42 @@
#include <sys/types.h>
#include <zlib.h>

static const char template[] =
"/* Generated file by tools/headerversions, do not edit! */\n"
"/* GMP version: %s */\n"
"/* SQLITE3 version: %u */\n"
"/* ZLIB version: %s */\n"
"#include <ccan/err/err.h>\n"
"#include <gmp.h>\n"
"#include <sqlite3.h>\n"
"#include <zlib.h>\n"
"\n"
"static inline void check_linked_library_versions(void)\n"
"{\n"
" char compiled_gmp_version[100];\n"
" if (SQLITE_VERSION_NUMBER != sqlite3_libversion_number())\n"
" errx(1, \"SQLITE version mismatch: compiled %%u, now %%u\",\n"
" SQLITE_VERSION_NUMBER, sqlite3_libversion_number());\n"
" /* zlib documents that first char alters ABI. Kudos! */\n"
" if (zlibVersion()[0] != ZLIB_VERSION[0])\n"
" errx(1, \"zlib version mismatch: compiled %%s, now %%s\",\n"
" ZLIB_VERSION, zlibVersion());\n"
" /* GMP doesn't say anything, and we have to assemble our own string. */\n"
" snprintf(compiled_gmp_version, sizeof(compiled_gmp_version),\n"
" \"%%u.%%u.%%u\",\n"
" __GNU_MP_VERSION,\n"
" __GNU_MP_VERSION_MINOR,\n"
" __GNU_MP_VERSION_PATCHLEVEL);\n"
" if (strcmp(compiled_gmp_version, gmp_version) != 0)\n"
" errx(1, \"gmp version mismatch: compiled %%s, now %%s\",\n"
" compiled_gmp_version, gmp_version);\n"
"}\n";

int main(int argc, char *argv[])
{
char *file, *new;

/* We don't bother with setup_locale(); we're a build tool */
err_set_progname(argv[0]);

if (argc != 2)
Expand All @@ -25,11 +57,7 @@ int main(int argc, char *argv[])
if (!file && errno != ENOENT)
err(1, "Reading %s", argv[1]);

new = tal_fmt(NULL,
"/* Generated file by tools/headerversions, do not edit! */\n"
"/* GMP version: %s */\n"
"/* SQLITE3 version: %u */\n"
"/* ZLIB version: %s */\n",
new = tal_fmt(NULL, template,
gmp_version,
sqlite3_libversion_number(),
zlibVersion());
Expand Down
4 changes: 0 additions & 4 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,6 @@ static struct db *db_open(const tal_t *ctx, char *filename)
struct db *db;
sqlite3 *sql;

if (SQLITE_VERSION_NUMBER != sqlite3_libversion_number())
db_fatal("SQLITE version mismatch: compiled %u, now %u",
SQLITE_VERSION_NUMBER, sqlite3_libversion_number());

int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
err = sqlite3_open_v2(filename, &sql, flags, NULL);

Expand Down

0 comments on commit 31a375a

Please sign in to comment.