| 1 |
import os |
| 2 |
import pytest |
| 3 |
from ..support import SUPPORTS_SYS_EXECUTABLE, SUPPORTS_MEM_PROTECTION |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
@pytest.fixture |
| 10 |
def hpy_abi(): |
| 11 |
from hpy.debug import LeakDetector |
| 12 |
with LeakDetector(): |
| 13 |
yield "debug" |
| 14 |
|
| 15 |
|
| 16 |
@pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess") |
| 17 |
def test_charptr_use_after_implicit_arg_handle_close(compiler, python_subprocess): |
| 18 |
mod = compiler.compile_module(""" |
| 19 |
const char *keep; |
| 20 |
|
| 21 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 22 |
static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t n) |
| 23 |
{ |
| 24 |
if (n != 2) { |
| 25 |
HPyErr_SetString(ctx, ctx->h_ValueError, "expected exactly two arguments"); |
| 26 |
return HPy_NULL; |
| 27 |
} |
| 28 |
long mode = HPyLong_AsLong(ctx, args[1]); |
| 29 |
if (mode == -1) |
| 30 |
return HPy_NULL; |
| 31 |
|
| 32 |
HPy_ssize_t size; |
| 33 |
if (mode == 0) { |
| 34 |
keep = HPyUnicode_AsUTF8AndSize(ctx, args[0], &size); |
| 35 |
} else if (mode == 1) { |
| 36 |
keep = HPyBytes_AsString(ctx, args[0]); |
| 37 |
} else if (mode == 2) { |
| 38 |
keep = HPyBytes_AS_STRING(ctx, args[0]); |
| 39 |
} else { |
| 40 |
HPyErr_SetString(ctx, ctx->h_ValueError, "invalid mode"); |
| 41 |
return HPy_NULL; |
| 42 |
} |
| 43 |
return HPy_Dup(ctx, ctx->h_None); |
| 44 |
} |
| 45 |
|
| 46 |
HPyDef_METH(g, "g", HPyFunc_NOARGS) |
| 47 |
static HPy g_impl(HPyContext *ctx, HPy self) |
| 48 |
{ |
| 49 |
return HPyUnicode_FromString(ctx, keep); |
| 50 |
} |
| 51 |
|
| 52 |
@EXPORT(f) |
| 53 |
@EXPORT(g) |
| 54 |
@INIT |
| 55 |
""") |
| 56 |
content = "'use after close me!'" |
| 57 |
bcontent = 'b' + content |
| 58 |
for mode, msg in enumerate((content, bcontent, bcontent)): |
| 59 |
if SUPPORTS_MEM_PROTECTION and False: |
| 60 |
code = "mod.f({}, {}); mod.g()".format(msg, mode) |
| 61 |
else: |
| 62 |
code = "mod.f({0}, {1}); assert mod.g() == str({0})".format(msg, mode) |
| 63 |
result = python_subprocess.run(mod, code) |
| 64 |
assert result.returncode != 0 |
| 65 |
assert result.stdout == b"" |
| 66 |
if SUPPORTS_MEM_PROTECTION: |
| 67 |
assert result.stderr == b"" |
| 68 |
else: |
| 69 |
|
| 70 |
assert b"UnicodeDecodeError" in result.stderr |
| 71 |
|
| 72 |
|
| 73 |
@pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess") |
| 74 |
def test_charptr_use_after_handle_close(compiler, python_subprocess): |
| 75 |
mod = compiler.compile_module(""" |
| 76 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 77 |
static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t n) |
| 78 |
{ |
| 79 |
if (n != 2) { |
| 80 |
HPyErr_SetString(ctx, ctx->h_ValueError, "expected exactly two arguments"); |
| 81 |
return HPy_NULL; |
| 82 |
} |
| 83 |
long mode = HPyLong_AsLong(ctx, args[1]); |
| 84 |
if (mode == -1) |
| 85 |
return HPy_NULL; |
| 86 |
|
| 87 |
HPy arg_dup = HPy_Dup(ctx, args[0]); |
| 88 |
HPy_ssize_t size; |
| 89 |
const char *keep; |
| 90 |
if (mode == 0) { |
| 91 |
keep = HPyUnicode_AsUTF8AndSize(ctx, arg_dup, &size); |
| 92 |
} else if (mode == 1) { |
| 93 |
keep = HPyBytes_AsString(ctx, arg_dup); |
| 94 |
} else if (mode == 2) { |
| 95 |
keep = HPyBytes_AS_STRING(ctx, arg_dup); |
| 96 |
} else { |
| 97 |
HPyErr_SetString(ctx, ctx->h_ValueError, "invalid mode"); |
| 98 |
return HPy_NULL; |
| 99 |
} |
| 100 |
HPy_Close(ctx, arg_dup); |
| 101 |
return HPyUnicode_FromString(ctx, keep); |
| 102 |
} |
| 103 |
|
| 104 |
@EXPORT(f) |
| 105 |
@INIT |
| 106 |
""") |
| 107 |
content = "'use after close me!'" |
| 108 |
bcontent = "b" + content |
| 109 |
for mode, msg in enumerate((content, bcontent, bcontent)): |
| 110 |
if SUPPORTS_MEM_PROTECTION: |
| 111 |
code = "mod.f({}, {})".format(msg, mode) |
| 112 |
else: |
| 113 |
code = "assert mod.f({0}, {1}) == str({0})".format(msg, mode) |
| 114 |
result = python_subprocess.run(mod, code) |
| 115 |
assert result.returncode != 0 |
| 116 |
assert result.stdout == b"" |
| 117 |
if SUPPORTS_MEM_PROTECTION: |
| 118 |
assert result.stderr == b"" |
| 119 |
else: |
| 120 |
|
| 121 |
assert b"UnicodeDecodeError" in result.stderr |
| 122 |
|
| 123 |
|
| 124 |
@pytest.mark.skipif(not SUPPORTS_MEM_PROTECTION, reason= |
| 125 |
"Could be implemented by checking the contents on close.") |
| 126 |
@pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess") |
| 127 |
def test_charptr_write_ptr(compiler, python_subprocess): |
| 128 |
mod = compiler.compile_module(""" |
| 129 |
HPyDef_METH(f, "f", HPyFunc_VARARGS) |
| 130 |
static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t n) |
| 131 |
{ |
| 132 |
if (n != 2) { |
| 133 |
HPyErr_SetString(ctx, ctx->h_ValueError, "expected exactly two arguments"); |
| 134 |
return HPy_NULL; |
| 135 |
} |
| 136 |
long mode = HPyLong_AsLong(ctx, args[1]); |
| 137 |
if (mode == -1) |
| 138 |
return HPy_NULL; |
| 139 |
|
| 140 |
HPy_ssize_t size; |
| 141 |
char *data; |
| 142 |
if (mode == 0) { |
| 143 |
data = (char *)HPyUnicode_AsUTF8AndSize(ctx, args[0], &size); |
| 144 |
} else if (mode == 1) { |
| 145 |
data = (char *)HPyBytes_AsString(ctx, args[0]); |
| 146 |
} else if (mode == 2) { |
| 147 |
data = (char *)HPyBytes_AS_STRING(ctx, args[0]); |
| 148 |
} else { |
| 149 |
HPyErr_SetString(ctx, ctx->h_ValueError, "invalid mode"); |
| 150 |
return HPy_NULL; |
| 151 |
} |
| 152 |
// write to read-only memory |
| 153 |
data[0] = 'a'; |
| 154 |
return HPy_Dup(ctx, ctx->h_None); |
| 155 |
} |
| 156 |
|
| 157 |
@EXPORT(f) |
| 158 |
@INIT |
| 159 |
""") |
| 160 |
content = "'try writing me!'" |
| 161 |
bcontent = "b" + content |
| 162 |
for mode, msg in enumerate((content, bcontent, bcontent)): |
| 163 |
result = python_subprocess.run(mod, "mod.f({}, {});".format(msg, mode)) |
| 164 |
assert result.returncode != 0 |
| 165 |
assert result.stdout == b"" |
| 166 |
assert result.stderr == b"" |
| 167 |
|
| 168 |
|
| 169 |
def test_charptr_correct_usage(compiler): |
| 170 |
mod = compiler.make_module(""" |
| 171 |
#include <string.h> |
| 172 |
#include <stdio.h> |
| 173 |
|
| 174 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 175 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 176 |
{ |
| 177 |
HPy arg_dup = HPy_Dup(ctx, arg); |
| 178 |
HPy_ssize_t size; |
| 179 |
const char *keep = HPyUnicode_AsUTF8AndSize(ctx, arg_dup, &size); |
| 180 |
char *copy = (char*) malloc(size + 1); |
| 181 |
memcpy(copy, keep, size + 1); |
| 182 |
HPy_Close(ctx, arg_dup); |
| 183 |
HPy result = HPyUnicode_FromString(ctx, copy); |
| 184 |
free(copy); |
| 185 |
return result; |
| 186 |
} |
| 187 |
|
| 188 |
@EXPORT(f) |
| 189 |
@INIT |
| 190 |
""") |
| 191 |
assert mod.f('I wont be leaked!') == 'I wont be leaked!'; |
| 192 |
|
| 193 |
|
| 194 |
def test_charptr_limit_stress_test(compiler): |
| 195 |
from hpy.universal import _debug |
| 196 |
mod = compiler.make_module(""" |
| 197 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 198 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 199 |
{ |
| 200 |
HPy_ssize_t size; |
| 201 |
HPyUnicode_AsUTF8AndSize(ctx, arg, &size); |
| 202 |
if (size == 0) { |
| 203 |
return HPy_NULL; |
| 204 |
} |
| 205 |
return HPy_Dup(ctx, ctx->h_None); |
| 206 |
} |
| 207 |
|
| 208 |
// Dummy function just to force handle creation, but should not create |
| 209 |
// any raw data attached to those handles |
| 210 |
HPyDef_METH(g, "g", HPyFunc_O) |
| 211 |
static HPy g_impl(HPyContext *ctx, HPy self, HPy arg) |
| 212 |
{ |
| 213 |
int len = HPyLong_AsLong(ctx, arg); |
| 214 |
for (int i = 0; i < len; ++i) { |
| 215 |
HPy h = HPyLong_FromLong(ctx, i); |
| 216 |
HPy_Close(ctx, h); |
| 217 |
} |
| 218 |
return HPy_Dup(ctx, ctx->h_None); |
| 219 |
} |
| 220 |
|
| 221 |
@EXPORT(f) |
| 222 |
@EXPORT(g) |
| 223 |
@INIT |
| 224 |
""") |
| 225 |
|
| 226 |
def get_raw_data_sizes(handles): |
| 227 |
return list(map(lambda h: h.raw_data_size, handles)) |
| 228 |
|
| 229 |
def clear_raw_data_in_closed_handles(): |
| 230 |
closed_size = len(_debug.get_closed_handles()) |
| 231 |
old_limit = _debug.get_closed_handles_queue_max_size() |
| 232 |
try: |
| 233 |
|
| 234 |
|
| 235 |
_debug.set_closed_handles_queue_max_size(closed_size) |
| 236 |
|
| 237 |
|
| 238 |
mod.g(closed_size - 2) |
| 239 |
finally: |
| 240 |
_debug.set_closed_handles_queue_max_size(old_limit) |
| 241 |
return closed_size |
| 242 |
|
| 243 |
old_raw_data_max_size = _debug.get_protected_raw_data_max_size() |
| 244 |
old_closed_handles_max_size = _debug.get_closed_handles_queue_max_size() |
| 245 |
_debug.set_protected_raw_data_max_size(100) |
| 246 |
try: |
| 247 |
|
| 248 |
closed_size = clear_raw_data_in_closed_handles() |
| 249 |
|
| 250 |
_debug.set_closed_handles_queue_max_size(closed_size + 100) |
| 251 |
|
| 252 |
initial = get_raw_data_sizes(_debug.get_closed_handles()) |
| 253 |
assert all(map(lambda x: x == -1, initial)) |
| 254 |
|
| 255 |
|
| 256 |
gen = _debug.new_generation() |
| 257 |
mod.f('abc' * 50) |
| 258 |
closed1 = get_raw_data_sizes(_debug.get_closed_handles(gen)) |
| 259 |
assert closed1 == [-1, -1, -1] |
| 260 |
|
| 261 |
|
| 262 |
gen = _debug.new_generation() |
| 263 |
mod.f('a' * 31) |
| 264 |
mod.f('b' * 32) |
| 265 |
mod.f('c' * 50) |
| 266 |
closed2 = get_raw_data_sizes(_debug.get_closed_handles(gen)) |
| 267 |
|
| 268 |
|
| 269 |
assert sorted(closed2) == [-1] * 7 + [32, 33] |
| 270 |
|
| 271 |
|
| 272 |
gen = _debug.new_generation() |
| 273 |
mod.f('a' * 13) |
| 274 |
closed3 = get_raw_data_sizes(_debug.get_closed_handles(gen)) |
| 275 |
|
| 276 |
assert sorted(closed3) == [-1, -1, 14] |
| 277 |
|
| 278 |
|
| 279 |
gen = _debug.new_generation() |
| 280 |
mod.f('a' * 27) |
| 281 |
closed4 = get_raw_data_sizes(_debug.get_closed_handles(gen)) |
| 282 |
|
| 283 |
assert sorted(closed4) == [-1, -1, -1] |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
closed_size = clear_raw_data_in_closed_handles() |
| 288 |
|
| 289 |
|
| 290 |
all_closed = _debug.get_closed_handles() |
| 291 |
assert len(all_closed) == closed_size |
| 292 |
for h in all_closed: |
| 293 |
assert h.raw_data_size == -1 |
| 294 |
finally: |
| 295 |
_debug.set_protected_raw_data_max_size(old_raw_data_max_size) |
| 296 |
_debug.set_closed_handles_queue_max_size(old_closed_handles_max_size) |