Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-45774: Autoconfiscate SQLite detection (GH-29507) #29507

Merged
merged 18 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-45774: Autoconfiscate SQLite detection
  • Loading branch information
Erlend E. Aasland committed Nov 9, 2021
commit f34b4820e1e2863194630c69f62110d8124c6141
127 changes: 126 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -10762,12 +10762,137 @@ fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_decimal_contextvar" >&5
$as_echo "$with_decimal_contextvar" >&6; }

for ac_header in sqlite3.h
do :
ac_fn_c_check_header_mongrel "$LINENO" "sqlite3.h" "ac_cv_header_sqlite3_h" "$ac_includes_default"
if test "x$ac_cv_header_sqlite3_h" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_SQLITE3_H 1
_ACEOF

save_LIBS=$LIBS
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for sqlite3_open_v2 in -lsqlite3" >&5
$as_echo_n "checking for sqlite3_open_v2 in -lsqlite3... " >&6; }
if ${ac_cv_lib_sqlite3_sqlite3_open_v2+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lsqlite3 $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */

/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */
#ifdef __cplusplus
extern "C"
#endif
char sqlite3_open_v2 ();
int
main ()
{
return sqlite3_open_v2 ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_sqlite3_sqlite3_open_v2=yes
else
ac_cv_lib_sqlite3_sqlite3_open_v2=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_sqlite3_sqlite3_open_v2" >&5
$as_echo "$ac_cv_lib_sqlite3_sqlite3_open_v2" >&6; }
if test "x$ac_cv_lib_sqlite3_sqlite3_open_v2" = xyes; then :

cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */


#include <sqlite3.h>
#if SQLITE_VERSION_NUMBER < 3007015
# error "SQLite 3.7.15 or higher required"
#endif

int
main ()
{

;
return 0;
}

_ACEOF
if ac_fn_c_try_compile "$LINENO"; then :

$as_echo "#define HAVE_LIBSQLITE3 1" >>confdefs.h

fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext

fi

{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for sqlite3_load_extension in -lsqlite3" >&5
$as_echo_n "checking for sqlite3_load_extension in -lsqlite3... " >&6; }
if ${ac_cv_lib_sqlite3_sqlite3_load_extension+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lsqlite3 $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */

/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */
#ifdef __cplusplus
extern "C"
#endif
char sqlite3_load_extension ();
int
main ()
{
return sqlite3_load_extension ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_sqlite3_sqlite3_load_extension=yes
else
ac_cv_lib_sqlite3_sqlite3_load_extension=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_sqlite3_sqlite3_load_extension" >&5
$as_echo "$ac_cv_lib_sqlite3_sqlite3_load_extension" >&6; }
if test "x$ac_cv_lib_sqlite3_sqlite3_load_extension" = xyes; then :
have_sqlite3_load_extension=yes
else
have_sqlite3_load_extension=no
fi

LIBS=$save_LIBS

fi

done


# Check for support for loadable sqlite extensions
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for --enable-loadable-sqlite-extensions" >&5
$as_echo_n "checking for --enable-loadable-sqlite-extensions... " >&6; }
# Check whether --enable-loadable-sqlite-extensions was given.
if test "${enable_loadable_sqlite_extensions+set}" = set; then :
enableval=$enable_loadable_sqlite_extensions;
enableval=$enable_loadable_sqlite_extensions; if test "x$have_sqlite3_load_extension" = xno; then :
as_fn_error $? "Your version of SQLite does not support loadable extensions" "$LINENO" 5
fi
else
enable_loadable_sqlite_extensions=no
fi
Expand Down
22 changes: 21 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3068,12 +3068,32 @@ fi

AC_MSG_RESULT($with_decimal_contextvar)

dnl Check for SQLite library
AC_CHECK_HEADERS([sqlite3.h], [
AS_VAR_COPY([save_LIBS], [LIBS])
AC_CHECK_LIB([sqlite3], [sqlite3_open_v2], [
AC_COMPILE_IFELSE([
AC_LANG_PROGRAM([
#include <sqlite3.h>
#if SQLITE_VERSION_NUMBER < 3007015
# error "SQLite 3.7.15 or higher required"
#endif
], [])
], [AC_DEFINE([HAVE_LIBSQLITE3], [1], [Define if SQLite > 3.7.15 is found])])
])
AC_CHECK_LIB([sqlite3], [sqlite3_load_extension],
[have_sqlite3_load_extension=yes],
[have_sqlite3_load_extension=no])
AS_VAR_COPY([LIBS], [save_LIBS])
])

# Check for support for loadable sqlite extensions
AC_MSG_CHECKING(for --enable-loadable-sqlite-extensions)
AC_ARG_ENABLE(loadable-sqlite-extensions,
AS_HELP_STRING([--enable-loadable-sqlite-extensions],
[support loadable extensions in _sqlite module, see Doc/library/sqlite3.rst (default is no)]),
[],
[AS_VAR_IF([have_sqlite3_load_extension], [no],
[AC_MSG_ERROR([Your version of SQLite does not support loadable extensions])])],
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
[enable_loadable_sqlite_extensions=no])
AC_MSG_RESULT($enable_loadable_sqlite_extensions)

Expand Down
6 changes: 6 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,9 @@
/* Define to 1 if you have the `sendfile' library (-lsendfile). */
#undef HAVE_LIBSENDFILE

/* Define if SQLite > 3.7.15 is found */
#undef HAVE_LIBSQLITE3

/* Define to 1 if you have the <libutil.h> header file. */
#undef HAVE_LIBUTIL_H

Expand Down Expand Up @@ -1028,6 +1031,9 @@
/* Define to 1 if you have the `splice' function. */
#undef HAVE_SPLICE

/* Define to 1 if you have the <sqlite3.h> header file. */
#undef HAVE_SQLITE3_H

/* Define if your compiler provides ssize_t */
#undef HAVE_SSIZE_T

Expand Down
118 changes: 17 additions & 101 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1506,108 +1506,24 @@ class db_found(Exception): pass
self.missing.append('_gdbm')

def detect_sqlite(self):
# The sqlite interface
sqlite_setup_debug = False # verbose debug prints from this script?

# We hunt for #define SQLITE_VERSION "n.n.n"
sqlite_incdir = sqlite_libdir = None
sqlite_inc_paths = [ '/usr/include',
'/usr/include/sqlite',
'/usr/include/sqlite3',
'/usr/local/include',
'/usr/local/include/sqlite',
'/usr/local/include/sqlite3',
]
if CROSS_COMPILING:
sqlite_inc_paths = []
MIN_SQLITE_VERSION_NUMBER = (3, 7, 15) # Issue 40810
MIN_SQLITE_VERSION = ".".join([str(x)
for x in MIN_SQLITE_VERSION_NUMBER])

# Scan the default include directories before the SQLite specific
# ones. This allows one to override the copy of sqlite on OSX,
# where /usr/include contains an old version of sqlite.
if MACOS:
sysroot = macosx_sdk_root()
have_sqlite3 = sysconfig.get_config_var("HAVE_LIBSQLITE3")
if not have_sqlite3:
self.missing.append("_sqlite3")
return

for d_ in self.inc_dirs + sqlite_inc_paths:
d = d_
if MACOS and is_macosx_sdk_path(d):
d = os.path.join(sysroot, d[1:])

f = os.path.join(d, "sqlite3.h")
if os.path.exists(f):
if sqlite_setup_debug: print("sqlite: found %s"%f)
with open(f) as file:
incf = file.read()
m = re.search(
r'\s*.*#\s*.*define\s.*SQLITE_VERSION\W*"([\d\.]*)"', incf)
if m:
sqlite_version = m.group(1)
sqlite_version_tuple = tuple([int(x)
for x in sqlite_version.split(".")])
if sqlite_version_tuple >= MIN_SQLITE_VERSION_NUMBER:
# we win!
if sqlite_setup_debug:
print("%s/sqlite3.h: version %s"%(d, sqlite_version))
sqlite_incdir = d
break
else:
if sqlite_setup_debug:
print("%s: version %s is too old, need >= %s"%(d,
sqlite_version, MIN_SQLITE_VERSION))
elif sqlite_setup_debug:
print("sqlite: %s had no SQLITE_VERSION"%(f,))

if sqlite_incdir:
sqlite_dirs_to_check = [
os.path.join(sqlite_incdir, '..', 'lib64'),
os.path.join(sqlite_incdir, '..', 'lib'),
os.path.join(sqlite_incdir, '..', '..', 'lib64'),
os.path.join(sqlite_incdir, '..', '..', 'lib'),
]
sqlite_libfile = self.compiler.find_library_file(
sqlite_dirs_to_check + self.lib_dirs, 'sqlite3')
if sqlite_libfile:
sqlite_libdir = [os.path.abspath(os.path.dirname(sqlite_libfile))]

if sqlite_incdir and sqlite_libdir:
sqlite_srcs = [
'_sqlite/connection.c',
'_sqlite/cursor.c',
'_sqlite/microprotocols.c',
'_sqlite/module.c',
'_sqlite/prepare_protocol.c',
'_sqlite/row.c',
'_sqlite/statement.c',
'_sqlite/util.c', ]
sqlite_defines = []

# Enable support for loadable extensions in the sqlite3 module
# if --enable-loadable-sqlite-extensions configure option is used.
if (
MACOS and
sqlite_incdir == os.path.join(MACOS_SDK_ROOT, "usr/include") and
sysconfig.get_config_var("PY_SQLITE_ENABLE_LOAD_EXTENSION")
):
raise DistutilsError("System version of SQLite does not support loadable extensions")

include_dirs = ["Modules/_sqlite"]
# Only include the directory where sqlite was found if it does
# not already exist in set include directories, otherwise you
# can end up with a bad search path order.
if sqlite_incdir not in self.compiler.include_dirs:
include_dirs.append(sqlite_incdir)
# avoid a runtime library path for a system library dir
if sqlite_libdir and sqlite_libdir[0] in self.lib_dirs:
sqlite_libdir = None
self.add(Extension('_sqlite3', sqlite_srcs,
define_macros=sqlite_defines,
include_dirs=include_dirs,
library_dirs=sqlite_libdir,
libraries=["sqlite3",]))
else:
self.missing.append('_sqlite3')
sqlite_srcs = [
"_sqlite/connection.c",
"_sqlite/cursor.c",
"_sqlite/microprotocols.c",
"_sqlite/module.c",
"_sqlite/prepare_protocol.c",
"_sqlite/row.c",
"_sqlite/statement.c",
"_sqlite/util.c",
]
self.add(Extension("_sqlite3", sqlite_srcs,
include_dirs=["Modules/_sqlite"],
libraries=["sqlite3",]))

def detect_platform_specific_exts(self):
# Unix-only modules
Expand Down