| 1 |
import pytest |
| 2 |
from hpy.debug.leakdetector import LeakDetector |
| 3 |
from ..support import SUPPORTS_SYS_EXECUTABLE, IS_PYTHON_DEBUG_BUILD |
| 4 |
from ..conftest import IS_VALGRIND_RUN |
| 5 |
import sys |
| 6 |
|
| 7 |
@pytest.fixture |
| 8 |
def hpy_abi(): |
| 9 |
with LeakDetector(): |
| 10 |
yield "debug" |
| 11 |
|
| 12 |
|
| 13 |
def test_no_invalid_handle(compiler, hpy_debug_capture): |
| 14 |
|
| 15 |
mod = compiler.make_module(""" |
| 16 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 17 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 18 |
{ |
| 19 |
HPy x = HPyLong_FromLong(ctx, 42); |
| 20 |
HPy y = HPyLong_FromLong(ctx, 2); |
| 21 |
HPy arg_dup = HPy_Dup(ctx, arg); |
| 22 |
HPy_Close(ctx, y); |
| 23 |
HPy b = HPy_Dup(ctx, x); |
| 24 |
HPy_Close(ctx, x); |
| 25 |
HPy_Close(ctx, arg_dup); |
| 26 |
return b; |
| 27 |
} |
| 28 |
|
| 29 |
@EXPORT(f) |
| 30 |
@INIT |
| 31 |
""") |
| 32 |
assert mod.f("hello") == 42 |
| 33 |
assert mod.f("world") == 42 |
| 34 |
assert hpy_debug_capture.invalid_handles_count == 0 |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.skipif(sys.implementation.name == 'pypy', |
| 38 |
reason="Cannot recover from use-after-close on pypy") |
| 39 |
def test_cant_use_closed_handle(compiler, hpy_debug_capture): |
| 40 |
mod = compiler.make_module(""" |
| 41 |
HPyDef_METH(f, "f", HPyFunc_O, .doc="double close") |
| 42 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 43 |
{ |
| 44 |
HPy h = HPy_Dup(ctx, arg); |
| 45 |
HPy_Close(ctx, h); |
| 46 |
HPy_Close(ctx, h); // double close |
| 47 |
return HPy_Dup(ctx, ctx->h_None); |
| 48 |
} |
| 49 |
|
| 50 |
HPyDef_METH(g, "g", HPyFunc_O, .doc="use after close") |
| 51 |
static HPy g_impl(HPyContext *ctx, HPy self, HPy arg) |
| 52 |
{ |
| 53 |
HPy h = HPy_Dup(ctx, arg); |
| 54 |
HPy_Close(ctx, h); |
| 55 |
return HPy_Repr(ctx, h); |
| 56 |
} |
| 57 |
|
| 58 |
HPyDef_METH(h, "h", HPyFunc_O, .doc="closing argument") |
| 59 |
static HPy h_impl(HPyContext *ctx, HPy self, HPy arg) |
| 60 |
{ |
| 61 |
// Argument is implicitly closed by the caller |
| 62 |
HPy_Close(ctx, arg); |
| 63 |
return HPy_Dup(ctx, ctx->h_None); |
| 64 |
} |
| 65 |
|
| 66 |
HPyDef_METH(f_noargs, "f_noargs", HPyFunc_NOARGS, .doc="returns arg w/o dupping it") |
| 67 |
static HPy f_noargs_impl(HPyContext *ctx, HPy self) |
| 68 |
{ |
| 69 |
// should be: return HPy_Dup(ctx, self); |
| 70 |
return self; |
| 71 |
} |
| 72 |
|
| 73 |
HPyDef_METH(f0, "f0", HPyFunc_O, .doc="returns arg w/o dupping it") |
| 74 |
static HPy f0_impl(HPyContext *ctx, HPy self, HPy arg) |
| 75 |
{ |
| 76 |
// should be: return HPy_Dup(ctx, arg); |
| 77 |
return arg; |
| 78 |
} |
| 79 |
|
| 80 |
HPyDef_METH(f_varargs, "f_varargs", HPyFunc_VARARGS, .doc="returns arg w/o dupping it") |
| 81 |
static HPy f_varargs_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs) |
| 82 |
{ |
| 83 |
// should be: return HPy_Dup(ctx, args[0]); |
| 84 |
return args[0]; |
| 85 |
} |
| 86 |
|
| 87 |
@EXPORT(f) |
| 88 |
@EXPORT(g) |
| 89 |
@EXPORT(f0) |
| 90 |
@EXPORT(f_noargs) |
| 91 |
@EXPORT(f_varargs) |
| 92 |
@EXPORT(h) |
| 93 |
@INIT |
| 94 |
""") |
| 95 |
mod.f('foo') |
| 96 |
assert hpy_debug_capture.invalid_handles_count == 1 |
| 97 |
mod.g('bar') |
| 98 |
assert hpy_debug_capture.invalid_handles_count == 2 |
| 99 |
if not IS_PYTHON_DEBUG_BUILD and not IS_VALGRIND_RUN: |
| 100 |
|
| 101 |
|
| 102 |
mod.f0('foo') |
| 103 |
assert hpy_debug_capture.invalid_handles_count == 3 |
| 104 |
mod.f_noargs() |
| 105 |
assert hpy_debug_capture.invalid_handles_count == 4 |
| 106 |
mod.f_varargs('foo', 'bar') |
| 107 |
assert hpy_debug_capture.invalid_handles_count == 5 |
| 108 |
mod.h('baz') |
| 109 |
assert hpy_debug_capture.invalid_handles_count == 6 |
| 110 |
|
| 111 |
@pytest.mark.skipif(True, |
| 112 |
reason="It's impossible to recover from use-after-close on pypy") |
| 113 |
def test_keeping_and_reusing_argument_handle(compiler, hpy_debug_capture): |
| 114 |
mod = compiler.make_module(""" |
| 115 |
HPy keep; |
| 116 |
|
| 117 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 118 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 119 |
{ |
| 120 |
keep = arg; |
| 121 |
return HPy_Dup(ctx, ctx->h_None); |
| 122 |
} |
| 123 |
|
| 124 |
HPyDef_METH(g, "g", HPyFunc_NOARGS) |
| 125 |
static HPy g_impl(HPyContext *ctx, HPy self) |
| 126 |
{ |
| 127 |
HPy_ssize_t len = HPy_Length(ctx, keep); |
| 128 |
return HPyLong_FromSsize_t(ctx, len); |
| 129 |
} |
| 130 |
|
| 131 |
@EXPORT(f) |
| 132 |
@EXPORT(g) |
| 133 |
@INIT |
| 134 |
""") |
| 135 |
s = "hello leaks!" |
| 136 |
mod.f(s) |
| 137 |
assert hpy_debug_capture.invalid_handles_count == 0 |
| 138 |
assert mod.g() == len(s) |
| 139 |
assert hpy_debug_capture.invalid_handles_count == 1 |
| 140 |
|
| 141 |
|
| 142 |
def test_return_ctx_constant_without_dup(compiler, python_subprocess, fatal_exit_code): |
| 143 |
|
| 144 |
|
| 145 |
if not SUPPORTS_SYS_EXECUTABLE: |
| 146 |
pytest.skip("no sys.executable") |
| 147 |
|
| 148 |
mod = compiler.compile_module(""" |
| 149 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 150 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 151 |
{ |
| 152 |
return ctx->h_None; |
| 153 |
} |
| 154 |
|
| 155 |
@EXPORT(f) |
| 156 |
@INIT |
| 157 |
""") |
| 158 |
result = python_subprocess.run(mod, "mod.f();") |
| 159 |
assert result.returncode == fatal_exit_code |
| 160 |
assert b"Invalid usage of already closed handle" in result.stderr |
| 161 |
|
| 162 |
|
| 163 |
def test_close_ctx_constant(compiler, python_subprocess, fatal_exit_code): |
| 164 |
|
| 165 |
|
| 166 |
if not SUPPORTS_SYS_EXECUTABLE: |
| 167 |
pytest.skip("no sys.executable") |
| 168 |
|
| 169 |
mod = compiler.compile_module(""" |
| 170 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 171 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 172 |
{ |
| 173 |
HPy_Close(ctx, ctx->h_True); |
| 174 |
return HPy_Dup(ctx, ctx->h_False); |
| 175 |
} |
| 176 |
|
| 177 |
@EXPORT(f) |
| 178 |
@INIT |
| 179 |
""") |
| 180 |
result = python_subprocess.run(mod, "mod.f();") |
| 181 |
assert result.returncode == fatal_exit_code |
| 182 |
assert b"Invalid usage of already closed handle" in result.stderr |
| 183 |
|
| 184 |
|
| 185 |
def test_invalid_handle_crashes_python_if_no_hook(compiler, python_subprocess, fatal_exit_code): |
| 186 |
if not SUPPORTS_SYS_EXECUTABLE: |
| 187 |
pytest.skip("no sys.executable") |
| 188 |
|
| 189 |
mod = compiler.compile_module(""" |
| 190 |
HPyDef_METH(f, "f", HPyFunc_O, .doc="double close") |
| 191 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 192 |
{ |
| 193 |
HPy h = HPy_Dup(ctx, arg); |
| 194 |
HPy_Close(ctx, h); |
| 195 |
HPy_Close(ctx, h); // double close |
| 196 |
return HPy_Dup(ctx, ctx->h_None); |
| 197 |
} |
| 198 |
|
| 199 |
@EXPORT(f) |
| 200 |
@INIT |
| 201 |
""") |
| 202 |
result = python_subprocess.run(mod, "mod.f(42);") |
| 203 |
assert result.returncode == fatal_exit_code |
| 204 |
assert b"Invalid usage of already closed handle" in result.stderr |