|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * The Universal Permissive License (UPL), Version 1.0
|
|
60 | 60 | import com.oracle.graal.python.builtins.modules.FcntlModuleBuiltinsClinicProviders.FlockNodeClinicProviderGen;
|
61 | 61 | import com.oracle.graal.python.builtins.modules.PosixModuleBuiltins.FileDescriptorConversionNode;
|
62 | 62 | import com.oracle.graal.python.builtins.objects.PNone;
|
| 63 | +import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary; |
| 64 | +import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAcquireLibrary; |
| 65 | +import com.oracle.graal.python.lib.PyLongAsIntNode; |
63 | 66 | import com.oracle.graal.python.lib.PyLongAsLongNode;
|
64 | 67 | import com.oracle.graal.python.nodes.ErrorMessages;
|
65 | 68 | import com.oracle.graal.python.nodes.PConstructAndRaiseNode;
|
|
68 | 71 | import com.oracle.graal.python.nodes.function.builtins.PythonBinaryClinicBuiltinNode;
|
69 | 72 | import com.oracle.graal.python.nodes.function.builtins.PythonClinicBuiltinNode;
|
70 | 73 | import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
|
| 74 | +import com.oracle.graal.python.nodes.util.CannotCastException; |
| 75 | +import com.oracle.graal.python.nodes.util.CastToTruffleStringNode; |
| 76 | +import com.oracle.graal.python.runtime.GilNode; |
| 77 | +import com.oracle.graal.python.runtime.IndirectCallData; |
71 | 78 | import com.oracle.graal.python.runtime.PosixConstants;
|
72 | 79 | import com.oracle.graal.python.runtime.PosixConstants.IntConstant;
|
73 | 80 | import com.oracle.graal.python.runtime.PosixSupportLibrary;
|
74 | 81 | import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
|
| 82 | +import com.oracle.graal.python.runtime.exception.PException; |
| 83 | +import com.oracle.graal.python.runtime.object.PythonObjectFactory; |
75 | 84 | import com.oracle.truffle.api.dsl.Bind;
|
76 | 85 | import com.oracle.truffle.api.dsl.Cached;
|
77 | 86 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
|
80 | 89 | import com.oracle.truffle.api.frame.VirtualFrame;
|
81 | 90 | import com.oracle.truffle.api.library.CachedLibrary;
|
82 | 91 | import com.oracle.truffle.api.nodes.Node;
|
| 92 | +import com.oracle.truffle.api.strings.TruffleString; |
83 | 93 |
|
84 | 94 | @CoreFunctions(defineModule = "fcntl")
|
85 | 95 | public final class FcntlModuleBuiltins extends PythonBuiltins {
|
@@ -176,4 +186,150 @@ protected ArgumentClinicProvider getArgumentClinic() {
|
176 | 186 | return FcntlModuleBuiltinsClinicProviders.LockfNodeClinicProviderGen.INSTANCE;
|
177 | 187 | }
|
178 | 188 | }
|
| 189 | + |
| 190 | + @Builtin(name = "ioctl", minNumOfPositionalArgs = 2, parameterNames = {"fd", "request", "arg", "mutate_flag"}) |
| 191 | + @ArgumentClinic(name = "fd", conversionClass = FileDescriptorConversionNode.class) |
| 192 | + @ArgumentClinic(name = "request", conversion = ClinicConversion.Long) |
| 193 | + @ArgumentClinic(name = "mutate_flag", conversion = ClinicConversion.Boolean, defaultValue = "true") |
| 194 | + @GenerateNodeFactory |
| 195 | + abstract static class IoctlNode extends PythonClinicBuiltinNode { |
| 196 | + private static final int IOCTL_BUFSZ = 1024; |
| 197 | + |
| 198 | + @Specialization |
| 199 | + Object ioctl(VirtualFrame frame, int fd, long request, Object arg, boolean mutateArg, |
| 200 | + @Bind("this") Node inliningTarget, |
| 201 | + @CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib, |
| 202 | + @CachedLibrary(limit = "3") PythonBufferAcquireLibrary acquireLib, |
| 203 | + @CachedLibrary(limit = "3") PythonBufferAccessLibrary bufferLib, |
| 204 | + @Cached("createFor(this)") IndirectCallData indirectCallData, |
| 205 | + @Cached PyLongAsIntNode asIntNode, |
| 206 | + @Cached CastToTruffleStringNode castToString, |
| 207 | + @Cached TruffleString.SwitchEncodingNode switchEncodingNode, |
| 208 | + @Cached TruffleString.CopyToByteArrayNode copyToByteArrayNode, |
| 209 | + @Cached GilNode gilNode, |
| 210 | + @Cached PRaiseNode.Lazy raiseNode, |
| 211 | + @Cached PConstructAndRaiseNode.Lazy constructAndRaiseNode, |
| 212 | + @Cached PythonObjectFactory factory, |
| 213 | + @Cached SysModuleBuiltins.AuditNode auditNode) { |
| 214 | + auditNode.audit(inliningTarget, "fcnt.ioctl", fd, request, arg); |
| 215 | + |
| 216 | + int intArg = 0; |
| 217 | + if (arg != PNone.NO_VALUE) { |
| 218 | + Object buffer = null; |
| 219 | + // Buffer argument |
| 220 | + if (acquireLib.hasBuffer(arg)) { |
| 221 | + boolean writable = false; |
| 222 | + try { |
| 223 | + buffer = acquireLib.acquireWritable(arg, frame, indirectCallData); |
| 224 | + writable = true; |
| 225 | + } catch (PException e) { |
| 226 | + try { |
| 227 | + buffer = acquireLib.acquireReadonly(arg, frame, indirectCallData); |
| 228 | + } catch (PException e1) { |
| 229 | + // ignore |
| 230 | + } |
| 231 | + } |
| 232 | + if (buffer != null) { |
| 233 | + try { |
| 234 | + int len = bufferLib.getBufferLength(buffer); |
| 235 | + boolean writeBack = false; |
| 236 | + boolean releaseGil = true; |
| 237 | + byte[] ioctlArg = null; |
| 238 | + if (writable && mutateArg) { |
| 239 | + writeBack = true; |
| 240 | + if (bufferLib.hasInternalByteArray(buffer)) { |
| 241 | + byte[] internalArray = bufferLib.getInternalByteArray(buffer); |
| 242 | + if (internalArray.length > len && internalArray[len] == 0) { |
| 243 | + writeBack = false; |
| 244 | + releaseGil = false; // Could resize concurrently |
| 245 | + ioctlArg = internalArray; |
| 246 | + } |
| 247 | + } |
| 248 | + } else { |
| 249 | + if (len > IOCTL_BUFSZ) { |
| 250 | + throw raiseNode.get(inliningTarget).raise(ValueError, ErrorMessages.IOCTL_STRING_ARG_TOO_LONG); |
| 251 | + } |
| 252 | + } |
| 253 | + if (ioctlArg == null) { |
| 254 | + ioctlArg = new byte[len + 1]; |
| 255 | + bufferLib.readIntoByteArray(buffer, 0, ioctlArg, 0, len); |
| 256 | + } |
| 257 | + try { |
| 258 | + int ret = callIoctlBytes(frame, inliningTarget, fd, request, ioctlArg, releaseGil, posixLib, gilNode, constructAndRaiseNode); |
| 259 | + if (writable && mutateArg) { |
| 260 | + return ret; |
| 261 | + } else { |
| 262 | + return factory.createBytes(ioctlArg, len); |
| 263 | + } |
| 264 | + } finally { |
| 265 | + if (writeBack) { |
| 266 | + bufferLib.writeFromByteArray(buffer, 0, ioctlArg, 0, len); |
| 267 | + } |
| 268 | + } |
| 269 | + } finally { |
| 270 | + bufferLib.release(buffer, frame, indirectCallData); |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | + // string arg |
| 275 | + TruffleString stringArg = null; |
| 276 | + try { |
| 277 | + stringArg = castToString.execute(inliningTarget, arg); |
| 278 | + } catch (CannotCastException e) { |
| 279 | + // ignore |
| 280 | + } |
| 281 | + if (stringArg != null) { |
| 282 | + TruffleString.Encoding utf8 = TruffleString.Encoding.UTF_8; |
| 283 | + stringArg = switchEncodingNode.execute(stringArg, utf8); |
| 284 | + int len = stringArg.byteLength(utf8); |
| 285 | + if (len > IOCTL_BUFSZ) { |
| 286 | + throw raiseNode.get(inliningTarget).raise(ValueError, ErrorMessages.IOCTL_STRING_ARG_TOO_LONG); |
| 287 | + } |
| 288 | + byte[] ioctlArg = new byte[len + 1]; |
| 289 | + copyToByteArrayNode.execute(stringArg, 0, ioctlArg, 0, len, utf8); |
| 290 | + callIoctlBytes(frame, inliningTarget, fd, request, ioctlArg, true, posixLib, gilNode, constructAndRaiseNode); |
| 291 | + return factory.createBytes(ioctlArg, len); |
| 292 | + } |
| 293 | + |
| 294 | + // int arg |
| 295 | + intArg = asIntNode.execute(frame, inliningTarget, arg); |
| 296 | + // fall through |
| 297 | + } |
| 298 | + |
| 299 | + // default arg or int arg |
| 300 | + try { |
| 301 | + gilNode.release(true); |
| 302 | + try { |
| 303 | + return posixLib.ioctlInt(getPosixSupport(), fd, request, intArg); |
| 304 | + } finally { |
| 305 | + gilNode.acquire(); |
| 306 | + } |
| 307 | + } catch (PosixException e) { |
| 308 | + throw constructAndRaiseNode.get(inliningTarget).raiseOSErrorFromPosixException(frame, e); |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + private int callIoctlBytes(VirtualFrame frame, Node inliningTarget, int fd, long request, byte[] ioctlArg, boolean releaseGil, PosixSupportLibrary posixLib, GilNode gilNode, |
| 313 | + PConstructAndRaiseNode.Lazy constructAndRaiseNode) { |
| 314 | + try { |
| 315 | + if (releaseGil) { |
| 316 | + gilNode.release(true); |
| 317 | + } |
| 318 | + try { |
| 319 | + return posixLib.ioctlBytes(getPosixSupport(), fd, request, ioctlArg); |
| 320 | + } finally { |
| 321 | + if (releaseGil) { |
| 322 | + gilNode.acquire(); |
| 323 | + } |
| 324 | + } |
| 325 | + } catch (PosixException e) { |
| 326 | + throw constructAndRaiseNode.get(inliningTarget).raiseOSErrorFromPosixException(frame, e); |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + @Override |
| 331 | + protected ArgumentClinicProvider getArgumentClinic() { |
| 332 | + return FcntlModuleBuiltinsClinicProviders.IoctlNodeClinicProviderGen.INSTANCE; |
| 333 | + } |
| 334 | + } |
179 | 335 | }
|
0 commit comments