| 1 |
import py |
| 2 |
import sys, os, time |
| 3 |
import struct |
| 4 |
import subprocess |
| 5 |
import signal |
| 6 |
|
| 7 |
from rpython.rtyper.lltypesystem import rffi |
| 8 |
from rpython.translator.interactive import Translation |
| 9 |
from rpython.translator.sandbox.sandlib import read_message, write_message |
| 10 |
from rpython.translator.sandbox.sandlib import write_exception |
| 11 |
|
| 12 |
if hasattr(signal, 'alarm'): |
| 13 |
_orig_read_message = read_message |
| 14 |
|
| 15 |
def _timed_out(*args): |
| 16 |
raise EOFError("timed out waiting for data") |
| 17 |
|
| 18 |
def read_message(f): |
| 19 |
signal.signal(signal.SIGALRM, _timed_out) |
| 20 |
signal.alarm(20) |
| 21 |
try: |
| 22 |
return _orig_read_message(f) |
| 23 |
finally: |
| 24 |
signal.alarm(0) |
| 25 |
signal.signal(signal.SIGALRM, signal.SIG_DFL) |
| 26 |
|
| 27 |
def expect(f, g, fnname, args, result, resulttype=None): |
| 28 |
msg = read_message(f) |
| 29 |
assert msg == fnname |
| 30 |
msg = read_message(f) |
| 31 |
assert msg == args |
| 32 |
assert [type(x) for x in msg] == [type(x) for x in args] |
| 33 |
if isinstance(result, Exception): |
| 34 |
write_exception(g, result) |
| 35 |
else: |
| 36 |
write_message(g, 0) |
| 37 |
write_message(g, result, resulttype) |
| 38 |
g.flush() |
| 39 |
|
| 40 |
def compile(f, gc='ref', **kwds): |
| 41 |
t = Translation(f, backend='c', sandbox=True, gc=gc, |
| 42 |
check_str_without_nul=True, **kwds) |
| 43 |
return str(t.compile()) |
| 44 |
|
| 45 |
def run_in_subprocess(exe): |
| 46 |
popen = subprocess.Popen(exe, stdin=subprocess.PIPE, |
| 47 |
stdout=subprocess.PIPE, |
| 48 |
stderr=subprocess.STDOUT) |
| 49 |
return popen.stdin, popen.stdout |
| 50 |
|
| 51 |
def test_open_dup(): |
| 52 |
def entry_point(argv): |
| 53 |
fd = os.open("/tmp/foobar", os.O_RDONLY, 0777) |
| 54 |
assert fd == 77 |
| 55 |
fd2 = os.dup(fd) |
| 56 |
assert fd2 == 78 |
| 57 |
return 0 |
| 58 |
|
| 59 |
exe = compile(entry_point) |
| 60 |
g, f = run_in_subprocess(exe) |
| 61 |
expect(f, g, "ll_os.ll_os_open", ("/tmp/foobar", os.O_RDONLY, 0777), 77) |
| 62 |
expect(f, g, "ll_os.ll_os_dup", (77, True), 78) |
| 63 |
g.close() |
| 64 |
tail = f.read() |
| 65 |
f.close() |
| 66 |
assert tail == "" |
| 67 |
|
| 68 |
def test_open_dup_rposix(): |
| 69 |
from rpython.rlib import rposix |
| 70 |
def entry_point(argv): |
| 71 |
fd = rposix.open("/tmp/foobar", os.O_RDONLY, 0777) |
| 72 |
assert fd == 77 |
| 73 |
fd2 = rposix.dup(fd) |
| 74 |
assert fd2 == 78 |
| 75 |
return 0 |
| 76 |
|
| 77 |
exe = compile(entry_point) |
| 78 |
g, f = run_in_subprocess(exe) |
| 79 |
expect(f, g, "ll_os.ll_os_open", ("/tmp/foobar", os.O_RDONLY, 0777), 77) |
| 80 |
expect(f, g, "ll_os.ll_os_dup", (77, True), 78) |
| 81 |
g.close() |
| 82 |
tail = f.read() |
| 83 |
f.close() |
| 84 |
assert tail == "" |
| 85 |
|
| 86 |
def test_read_write(): |
| 87 |
def entry_point(argv): |
| 88 |
fd = os.open("/tmp/foobar", os.O_RDONLY, 0777) |
| 89 |
assert fd == 77 |
| 90 |
res = os.read(fd, 123) |
| 91 |
assert res == "he\x00llo" |
| 92 |
count = os.write(fd, "world\x00!\x00") |
| 93 |
assert count == 42 |
| 94 |
os.close(fd) |
| 95 |
return 0 |
| 96 |
|
| 97 |
exe = compile(entry_point) |
| 98 |
g, f = run_in_subprocess(exe) |
| 99 |
expect(f, g, "ll_os.ll_os_open", ("/tmp/foobar", os.O_RDONLY, 0777), 77) |
| 100 |
expect(f, g, "ll_os.ll_os_read", (77, 123), "he\x00llo") |
| 101 |
expect(f, g, "ll_os.ll_os_write", (77, "world\x00!\x00"), 42) |
| 102 |
expect(f, g, "ll_os.ll_os_close", (77,), None) |
| 103 |
g.close() |
| 104 |
tail = f.read() |
| 105 |
f.close() |
| 106 |
assert tail == "" |
| 107 |
|
| 108 |
def test_dup2_access(): |
| 109 |
def entry_point(argv): |
| 110 |
os.dup2(34, 56) |
| 111 |
y = os.access("spam", 77) |
| 112 |
return 1 - y |
| 113 |
|
| 114 |
exe = compile(entry_point) |
| 115 |
g, f = run_in_subprocess(exe) |
| 116 |
expect(f, g, "ll_os.ll_os_dup2", (34, 56, True), None) |
| 117 |
expect(f, g, "ll_os.ll_os_access", ("spam", 77), True) |
| 118 |
g.close() |
| 119 |
tail = f.read() |
| 120 |
f.close() |
| 121 |
assert tail == "" |
| 122 |
|
| 123 |
def test_stat_ftruncate(): |
| 124 |
from rpython.translator.sandbox.sandlib import RESULTTYPE_STATRESULT |
| 125 |
from rpython.rlib.rarithmetic import r_longlong |
| 126 |
r0x12380000007 = r_longlong(0x12380000007) |
| 127 |
|
| 128 |
if not hasattr(os, 'ftruncate'): |
| 129 |
py.test.skip("posix only") |
| 130 |
|
| 131 |
def entry_point(argv): |
| 132 |
st = os.stat("somewhere") |
| 133 |
os.ftruncate(st.st_mode, st.st_size) |
| 134 |
return 0 |
| 135 |
|
| 136 |
exe = compile(entry_point) |
| 137 |
g, f = run_in_subprocess(exe) |
| 138 |
st = os.stat_result((55, 0, 0, 0, 0, 0, 0x12380000007, 0, 0, 0)) |
| 139 |
expect(f, g, "ll_os.ll_os_stat", ("somewhere",), st, |
| 140 |
resulttype = RESULTTYPE_STATRESULT) |
| 141 |
expect(f, g, "ll_os.ll_os_ftruncate", (55, 0x12380000007), None) |
| 142 |
g.close() |
| 143 |
tail = f.read() |
| 144 |
f.close() |
| 145 |
assert tail == "" |
| 146 |
|
| 147 |
def test_time(): |
| 148 |
def entry_point(argv): |
| 149 |
t = time.time() |
| 150 |
os.dup(int(t*1000)) |
| 151 |
return 0 |
| 152 |
|
| 153 |
exe = compile(entry_point) |
| 154 |
g, f = run_in_subprocess(exe) |
| 155 |
expect(f, g, "ll_time.ll_time_time", (), 3.141592) |
| 156 |
expect(f, g, "ll_os.ll_os_dup", (3141, True), 3) |
| 157 |
g.close() |
| 158 |
tail = f.read() |
| 159 |
f.close() |
| 160 |
assert tail == "" |
| 161 |
|
| 162 |
def test_getcwd(): |
| 163 |
def entry_point(argv): |
| 164 |
t = os.getcwd() |
| 165 |
os.dup(len(t)) |
| 166 |
return 0 |
| 167 |
|
| 168 |
exe = compile(entry_point) |
| 169 |
g, f = run_in_subprocess(exe) |
| 170 |
expect(f, g, "ll_os.ll_os_getcwd", (), "/tmp/foo/bar") |
| 171 |
expect(f, g, "ll_os.ll_os_dup", (len("/tmp/foo/bar"), True), 3) |
| 172 |
g.close() |
| 173 |
tail = f.read() |
| 174 |
f.close() |
| 175 |
assert tail == "" |
| 176 |
|
| 177 |
def test_oserror(): |
| 178 |
def entry_point(argv): |
| 179 |
try: |
| 180 |
os.stat("somewhere") |
| 181 |
except OSError as e: |
| 182 |
os.close(e.errno) |
| 183 |
return 0 |
| 184 |
|
| 185 |
exe = compile(entry_point) |
| 186 |
g, f = run_in_subprocess(exe) |
| 187 |
expect(f, g, "ll_os.ll_os_stat", ("somewhere",), OSError(6321, "egg")) |
| 188 |
expect(f, g, "ll_os.ll_os_close", (6321,), None) |
| 189 |
g.close() |
| 190 |
tail = f.read() |
| 191 |
f.close() |
| 192 |
assert tail == "" |
| 193 |
|
| 194 |
def test_hybrid_gc(): |
| 195 |
def entry_point(argv): |
| 196 |
l = [] |
| 197 |
for i in range(int(argv[1])): |
| 198 |
l.append("x" * int(argv[2])) |
| 199 |
return int(len(l) > 1000) |
| 200 |
|
| 201 |
exe = compile(entry_point, gc='hybrid', lldebug=True) |
| 202 |
pipe = subprocess.Popen([exe, '10', '10000'], stdout=subprocess.PIPE, |
| 203 |
stdin=subprocess.PIPE) |
| 204 |
g = pipe.stdin |
| 205 |
f = pipe.stdout |
| 206 |
expect(f, g, "ll_os.ll_os_getenv", ("PYPY_GENERATIONGC_NURSERY",), None) |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
expect(f, g, "ll_os.ll_os_getenv", ("PYPY_GC_DEBUG",), None) |
| 211 |
g.close() |
| 212 |
tail = f.read() |
| 213 |
f.close() |
| 214 |
assert tail == "" |
| 215 |
rescode = pipe.wait() |
| 216 |
assert rescode == 0 |
| 217 |
|
| 218 |
def test_segfault_1(): |
| 219 |
class A: |
| 220 |
def __init__(self, m): |
| 221 |
self.m = m |
| 222 |
def g(m): |
| 223 |
if m < 10: |
| 224 |
return None |
| 225 |
return A(m) |
| 226 |
def entry_point(argv): |
| 227 |
x = g(len(argv)) |
| 228 |
return int(x.m) |
| 229 |
|
| 230 |
exe = compile(entry_point) |
| 231 |
g, f = run_in_subprocess(exe) |
| 232 |
g.close() |
| 233 |
tail = f.read() |
| 234 |
f.close() |
| 235 |
assert 'Invalid RPython operation' in tail |
| 236 |
|
| 237 |
def test_segfault_2(): |
| 238 |
py.test.skip("hum, this is one example, but we need to be very careful") |
| 239 |
class Base: |
| 240 |
pass |
| 241 |
class A(Base): |
| 242 |
def __init__(self, m): |
| 243 |
self.m = m |
| 244 |
def getm(self): |
| 245 |
return self.m |
| 246 |
class B(Base): |
| 247 |
def __init__(self, a): |
| 248 |
self.a = a |
| 249 |
def g(m): |
| 250 |
a = A(m) |
| 251 |
if m < 10: |
| 252 |
a = B(a) |
| 253 |
return a |
| 254 |
def entry_point(argv): |
| 255 |
x = g(len(argv)) |
| 256 |
os.write(2, str(x.getm())) |
| 257 |
return 0 |
| 258 |
|
| 259 |
exe = compile(entry_point) |
| 260 |
g, f, e = os.popen3(exe, "t", 0) |
| 261 |
g.close() |
| 262 |
tail = f.read(23) |
| 263 |
f.close() |
| 264 |
assert tail == "" |
| 265 |
errors = e.read() |
| 266 |
e.close() |
| 267 |
assert '...think what kind of errors to get...' in errors |
| 268 |
|
| 269 |
def test_safe_alloc(): |
| 270 |
from rpython.rlib.rmmap import alloc, free |
| 271 |
|
| 272 |
def entry_point(argv): |
| 273 |
one = alloc(1024) |
| 274 |
free(one, 1024) |
| 275 |
return 0 |
| 276 |
|
| 277 |
exe = compile(entry_point) |
| 278 |
pipe = subprocess.Popen([exe], stdout=subprocess.PIPE, |
| 279 |
stdin=subprocess.PIPE) |
| 280 |
g = pipe.stdin |
| 281 |
f = pipe.stdout |
| 282 |
g.close() |
| 283 |
tail = f.read() |
| 284 |
f.close() |
| 285 |
assert tail == "" |
| 286 |
rescode = pipe.wait() |
| 287 |
assert rescode == 0 |
| 288 |
|
| 289 |
def test_unsafe_mmap(): |
| 290 |
py.test.skip("Since this stuff is unimplemented, it won't work anyway " |
| 291 |
"however, the day it starts working, it should pass test") |
| 292 |
from rpython.rlib.rmmap import mmap |
| 293 |
|
| 294 |
def entry_point(argv): |
| 295 |
try: |
| 296 |
res = mmap(0, 1024) |
| 297 |
except OSError: |
| 298 |
return 0 |
| 299 |
return 1 |
| 300 |
|
| 301 |
exe = compile(entry_point) |
| 302 |
pipe = subprocess.Popen([exe], stdout=subprocess.PIPE, |
| 303 |
stdin=subprocess.PIPE) |
| 304 |
g = pipe.stdin |
| 305 |
f = pipe.stdout |
| 306 |
expect(f, g, "mmap", ARGS, OSError(1, "xyz")) |
| 307 |
g.close() |
| 308 |
tail = f.read() |
| 309 |
f.close() |
| 310 |
assert tail == "" |
| 311 |
rescode = pipe.wait() |
| 312 |
assert rescode == 0 |
| 313 |
|
| 314 |
def test_environ_items(): |
| 315 |
def entry_point(argv): |
| 316 |
print os.environ.items() |
| 317 |
return 0 |
| 318 |
|
| 319 |
exe = compile(entry_point) |
| 320 |
g, f = run_in_subprocess(exe) |
| 321 |
expect(f, g, "ll_os.ll_os_envitems", (), []) |
| 322 |
expect(f, g, "ll_os.ll_os_write", (1, "[]\n"), 3) |
| 323 |
g.close() |
| 324 |
tail = f.read() |
| 325 |
f.close() |
| 326 |
assert tail == "" |
| 327 |
|
| 328 |
|
| 329 |
class TestPrintedResults: |
| 330 |
|
| 331 |
def run(self, entry_point, args, expected): |
| 332 |
exe = compile(entry_point) |
| 333 |
from rpython.translator.sandbox.sandlib import SimpleIOSandboxedProc |
| 334 |
proc = SimpleIOSandboxedProc([exe] + args) |
| 335 |
output, error = proc.communicate() |
| 336 |
assert error == '' |
| 337 |
assert output == expected |
| 338 |
|
| 339 |
def test_safefuncs(self): |
| 340 |
import math |
| 341 |
def entry_point(argv): |
| 342 |
a = float(argv[1]) |
| 343 |
print int(math.floor(a - 0.2)), |
| 344 |
print int(math.ceil(a)), |
| 345 |
print int(100.0 * math.sin(a)), |
| 346 |
mantissa, exponent = math.frexp(a) |
| 347 |
print int(100.0 * mantissa), exponent, |
| 348 |
fracpart, intpart = math.modf(a) |
| 349 |
print int(100.0 * fracpart), int(intpart), |
| 350 |
print |
| 351 |
return 0 |
| 352 |
self.run(entry_point, ["3.011"], "2 4 13 75 2 1 3\n") |
| 353 |
|
| 354 |
def test_safefuncs_exception(self): |
| 355 |
import math |
| 356 |
def entry_point(argv): |
| 357 |
a = float(argv[1]) |
| 358 |
x = math.log(a) |
| 359 |
print int(x * 100.0) |
| 360 |
try: |
| 361 |
math.log(-a) |
| 362 |
except ValueError: |
| 363 |
print 'as expected, got a ValueError' |
| 364 |
else: |
| 365 |
print 'did not get a ValueError!' |
| 366 |
return 0 |
| 367 |
self.run(entry_point, ["3.011"], "110\nas expected, got a ValueError\n") |
| 368 |
|
| 369 |
def test_os_path_safe(self): |
| 370 |
def entry_point(argv): |
| 371 |
print os.path.join('tmp', argv[1]) |
| 372 |
return 0 |
| 373 |
self.run(entry_point, ["spam"], os.path.join("tmp", "spam")+'\n') |