Skip to content

Commit 60c7003

Browse files
committed
Raise error if ioctl is not available
1 parent 2e0d742 commit 60c7003

File tree

3 files changed

+94
-80
lines changed

3 files changed

+94
-80
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/FcntlModuleBuiltins.java

Lines changed: 82 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -213,99 +213,103 @@ Object ioctl(VirtualFrame frame, int fd, long request, Object arg, boolean mutat
213213
@Cached SysModuleBuiltins.AuditNode auditNode) {
214214
auditNode.audit(inliningTarget, "fcnt.ioctl", fd, request, arg);
215215

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) {
216+
try {
217+
int intArg = 0;
218+
if (arg != PNone.NO_VALUE) {
219+
Object buffer = null;
220+
// Buffer argument
221+
if (acquireLib.hasBuffer(arg)) {
222+
boolean writable = false;
233223
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);
224+
buffer = acquireLib.acquireWritable(arg, frame, indirectCallData);
225+
writable = true;
226+
} catch (PException e) {
227+
try {
228+
buffer = acquireLib.acquireReadonly(arg, frame, indirectCallData);
229+
} catch (PException e1) {
230+
// ignore
256231
}
232+
}
233+
if (buffer != null) {
257234
try {
258-
int ret = callIoctlBytes(frame, inliningTarget, fd, request, ioctlArg, releaseGil, posixLib, gilNode, constructAndRaiseNode);
235+
int len = bufferLib.getBufferLength(buffer);
236+
boolean writeBack = false;
237+
boolean releaseGil = true;
238+
byte[] ioctlArg = null;
259239
if (writable && mutateArg) {
260-
return ret;
240+
writeBack = true;
241+
if (bufferLib.hasInternalByteArray(buffer)) {
242+
byte[] internalArray = bufferLib.getInternalByteArray(buffer);
243+
if (internalArray.length > len && internalArray[len] == 0) {
244+
writeBack = false;
245+
releaseGil = false; // Could resize concurrently
246+
ioctlArg = internalArray;
247+
}
248+
}
261249
} else {
262-
return factory.createBytes(ioctlArg, len);
250+
if (len > IOCTL_BUFSZ) {
251+
throw raiseNode.get(inliningTarget).raise(ValueError, ErrorMessages.IOCTL_STRING_ARG_TOO_LONG);
252+
}
263253
}
264-
} finally {
265-
if (writeBack) {
266-
bufferLib.writeFromByteArray(buffer, 0, ioctlArg, 0, len);
254+
if (ioctlArg == null) {
255+
ioctlArg = new byte[len + 1];
256+
bufferLib.readIntoByteArray(buffer, 0, ioctlArg, 0, len);
267257
}
258+
try {
259+
int ret = callIoctlBytes(frame, inliningTarget, fd, request, ioctlArg, releaseGil, posixLib, gilNode, constructAndRaiseNode);
260+
if (writable && mutateArg) {
261+
return ret;
262+
} else {
263+
return factory.createBytes(ioctlArg, len);
264+
}
265+
} finally {
266+
if (writeBack) {
267+
bufferLib.writeFromByteArray(buffer, 0, ioctlArg, 0, len);
268+
}
269+
}
270+
} finally {
271+
bufferLib.release(buffer, frame, indirectCallData);
268272
}
269-
} finally {
270-
bufferLib.release(buffer, frame, indirectCallData);
271273
}
272274
}
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);
275+
// string arg
276+
TruffleString stringArg = null;
277+
try {
278+
stringArg = castToString.execute(inliningTarget, arg);
279+
} catch (CannotCastException e) {
280+
// ignore
281+
}
282+
if (stringArg != null) {
283+
TruffleString.Encoding utf8 = TruffleString.Encoding.UTF_8;
284+
stringArg = switchEncodingNode.execute(stringArg, utf8);
285+
int len = stringArg.byteLength(utf8);
286+
if (len > IOCTL_BUFSZ) {
287+
throw raiseNode.get(inliningTarget).raise(ValueError, ErrorMessages.IOCTL_STRING_ARG_TOO_LONG);
288+
}
289+
byte[] ioctlArg = new byte[len + 1];
290+
copyToByteArrayNode.execute(stringArg, 0, ioctlArg, 0, len, utf8);
291+
callIoctlBytes(frame, inliningTarget, fd, request, ioctlArg, true, posixLib, gilNode, constructAndRaiseNode);
292+
return factory.createBytes(ioctlArg, len);
287293
}
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-
}
293294

294-
// int arg
295-
intArg = asIntNode.execute(frame, inliningTarget, arg);
296-
// fall through
297-
}
295+
// int arg
296+
intArg = asIntNode.execute(frame, inliningTarget, arg);
297+
// fall through
298+
}
298299

299-
// default arg or int arg
300-
try {
301-
gilNode.release(true);
300+
// default arg or int arg
302301
try {
303-
return posixLib.ioctlInt(getPosixSupport(), fd, request, intArg);
304-
} finally {
305-
gilNode.acquire();
302+
gilNode.release(true);
303+
try {
304+
return posixLib.ioctlInt(getPosixSupport(), fd, request, intArg);
305+
} finally {
306+
gilNode.acquire();
307+
}
308+
} catch (PosixException e) {
309+
throw constructAndRaiseNode.get(inliningTarget).raiseOSErrorFromPosixException(frame, e);
306310
}
307-
} catch (PosixException e) {
308-
throw constructAndRaiseNode.get(inliningTarget).raiseOSErrorFromPosixException(frame, e);
311+
} catch (PosixSupportLibrary.UnsupportedPosixFeatureException e) {
312+
throw constructAndRaiseNode.get(inliningTarget).raiseOSErrorUnsupported(frame, e);
309313
}
310314
}
311315

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PConstructAndRaiseNode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import com.oracle.graal.python.nodes.PConstructAndRaiseNodeGen.LazyNodeGen;
5757
import com.oracle.graal.python.nodes.call.special.CallVarargsMethodNode;
5858
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
59+
import com.oracle.graal.python.runtime.PosixSupportLibrary.UnsupportedPosixFeatureException;
5960
import com.oracle.graal.python.runtime.PythonContext;
6061
import com.oracle.graal.python.runtime.exception.PException;
6162
import com.oracle.graal.python.runtime.formatting.ErrorMessageFormatter;
@@ -226,6 +227,15 @@ public final PException raiseOSErrorFromPosixException(VirtualFrame frame, Posix
226227
return raiseOSError(frame, e.getErrorCode(), e.getMessageAsTruffleString(), filename1, filename2);
227228
}
228229

230+
public final PException raiseOSErrorUnsupported(VirtualFrame frame, UnsupportedPosixFeatureException e) {
231+
return raiseOSError(frame, OSErrorEnum.EINVAL, createUnsupportedErrorMessage(e));
232+
}
233+
234+
@TruffleBoundary
235+
private static TruffleString createUnsupportedErrorMessage(UnsupportedPosixFeatureException e) {
236+
return TruffleString.fromJavaStringUncached(e.getMessage(), TS_ENCODING);
237+
}
238+
229239
public final PException raiseSSLError(Frame frame, TruffleString message) {
230240
return raiseSSLError(frame, message, PythonUtils.EMPTY_OBJECT_ARRAY);
231241
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/EmulatedPosixSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2849,13 +2849,13 @@ private static PwdResult createPwdResult(UnixSystem unix) {
28492849
@ExportMessage
28502850
@SuppressWarnings("unused")
28512851
public int ioctlBytes(int fd, long request, byte[] arg) {
2852-
throw new UnsupportedPosixFeatureException("ioctl");
2852+
throw new UnsupportedPosixFeatureException("ioctl is not available in Java posix backend");
28532853
}
28542854

28552855
@ExportMessage
28562856
@SuppressWarnings("unused")
28572857
public int ioctlInt(int fd, long request, int arg) {
2858-
throw new UnsupportedPosixFeatureException("ioctl");
2858+
throw new UnsupportedPosixFeatureException("ioctl is not available in Java posix backend");
28592859
}
28602860

28612861
@ExportMessage

0 commit comments

Comments
 (0)