|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import unittest |
| 4 | +import platform |
| 5 | + |
| 6 | +FOO_C = r""" |
| 7 | +#include <unistd.h> |
| 8 | +
|
| 9 | +/* This is a 'GNU indirect function' (IFUNC) that will be called by |
| 10 | + dlsym() to resolve the symbol "foo" to an address. Typically, such |
| 11 | + a function would return the address of an actual function, but it |
| 12 | + can also just return NULL. For some background on IFUNCs, see |
| 13 | + https://willnewton.name/uncategorized/using-gnu-indirect-functions. |
| 14 | +
|
| 15 | + Adapted from Michael Kerrisk's answer: https://stackoverflow.com/a/53590014. |
| 16 | +*/ |
| 17 | +
|
| 18 | +asm (".type foo STT_GNU_IFUNC"); |
| 19 | +
|
| 20 | +void *foo(void) |
| 21 | +{ |
| 22 | + write($DESCRIPTOR, "OK", 2); |
| 23 | + return NULL; |
| 24 | +} |
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +@unittest.skipUnless(sys.platform.startswith('linux'), |
| 29 | + 'Test only valid for Linux') |
| 30 | +class TestNullDlsym(unittest.TestCase): |
| 31 | + """GH-126554: Ensure that we catch NULL dlsym return values |
| 32 | +
|
| 33 | + In rare cases, such as when using GNU IFUNCs, dlsym(), |
| 34 | + the C function that ctypes' CDLL uses to get the address |
| 35 | + of symbols, can return NULL. |
| 36 | +
|
| 37 | + The objective way of telling if an error during symbol |
| 38 | + lookup happened is to call glibc's dlerror() and check |
| 39 | + for a non-NULL return value. |
| 40 | +
|
| 41 | + However, there can be cases where dlsym() returns NULL |
| 42 | + and dlerror() is also NULL, meaning that glibc did not |
| 43 | + encounter any error. |
| 44 | +
|
| 45 | + In the case of ctypes, we subjectively treat that as |
| 46 | + an error, and throw a relevant exception. |
| 47 | +
|
| 48 | + This test case ensures that we correctly enforce |
| 49 | + this 'dlsym returned NULL -> throw Error' rule. |
| 50 | + """ |
| 51 | + |
| 52 | + def test_null_dlsym(self): |
| 53 | + import subprocess |
| 54 | + import tempfile |
| 55 | + |
| 56 | + # To avoid ImportErrors on Windows, where _ctypes does not have |
| 57 | + # dlopen and dlsym, |
| 58 | + # import here, i.e., inside the test function. |
| 59 | + # The skipUnless('linux') decorator ensures that we're on linux |
| 60 | + # if we're executing these statements. |
| 61 | + from ctypes import CDLL, c_int |
| 62 | + from _ctypes import dlopen, dlsym |
| 63 | + |
| 64 | + retcode = subprocess.call(["gcc", "--version"], |
| 65 | + stdout=subprocess.DEVNULL, |
| 66 | + stderr=subprocess.DEVNULL) |
| 67 | + if retcode != 0: |
| 68 | + self.skipTest("gcc is missing") |
| 69 | + |
| 70 | + pipe_r, pipe_w = os.pipe() |
| 71 | + self.addCleanup(os.close, pipe_r) |
| 72 | + self.addCleanup(os.close, pipe_w) |
| 73 | + |
| 74 | + with tempfile.TemporaryDirectory() as d: |
| 75 | + # Create a C file with a GNU Indirect Function (FOO_C) |
| 76 | + # and compile it into a shared library. |
| 77 | + srcname = os.path.join(d, 'foo.c') |
| 78 | + dstname = os.path.join(d, 'libfoo.so') |
| 79 | + with open(srcname, 'w') as f: |
| 80 | + f.write(FOO_C.replace('$DESCRIPTOR', str(pipe_w))) |
| 81 | + args = ['gcc', '-fPIC', '-shared', '-o', dstname, srcname] |
| 82 | + p = subprocess.run(args, capture_output=True) |
| 83 | + |
| 84 | + if p.returncode != 0: |
| 85 | + # IFUNC is not supported on all architectures. |
| 86 | + if platform.machine() == 'x86_64': |
| 87 | + # It should be supported here. Something else went wrong. |
| 88 | + p.check_returncode() |
| 89 | + else: |
| 90 | + # IFUNC might not be supported on this machine. |
| 91 | + self.skipTest(f"could not compile indirect function: {p}") |
| 92 | + |
| 93 | + # Case #1: Test 'PyCFuncPtr_FromDll' from Modules/_ctypes/_ctypes.c |
| 94 | + L = CDLL(dstname) |
| 95 | + with self.assertRaisesRegex(AttributeError, "function 'foo' not found"): |
| 96 | + # Try accessing the 'foo' symbol. |
| 97 | + # It should resolve via dlsym() to NULL, |
| 98 | + # and since we subjectively treat NULL |
| 99 | + # addresses as errors, we should get |
| 100 | + # an error. |
| 101 | + L.foo |
| 102 | + |
| 103 | + # Assert that the IFUNC was called |
| 104 | + self.assertEqual(os.read(pipe_r, 2), b'OK') |
| 105 | + |
| 106 | + # Case #2: Test 'CDataType_in_dll_impl' from Modules/_ctypes/_ctypes.c |
| 107 | + with self.assertRaisesRegex(ValueError, "symbol 'foo' not found"): |
| 108 | + c_int.in_dll(L, "foo") |
| 109 | + |
| 110 | + # Assert that the IFUNC was called |
| 111 | + self.assertEqual(os.read(pipe_r, 2), b'OK') |
| 112 | + |
| 113 | + # Case #3: Test 'py_dl_sym' from Modules/_ctypes/callproc.c |
| 114 | + L = dlopen(dstname) |
| 115 | + with self.assertRaisesRegex(OSError, "symbol 'foo' not found"): |
| 116 | + dlsym(L, "foo") |
| 117 | + |
| 118 | + # Assert that the IFUNC was called |
| 119 | + self.assertEqual(os.read(pipe_r, 2), b'OK') |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + unittest.main() |
0 commit comments