Skip to content

Commit 692bb88

Browse files
committed
[GR-25988] Add missing TruffleBoundaries
PullRequest: graalpython/1255
2 parents a83b28d + d4985c7 commit 692bb88

File tree

15 files changed

+95
-44
lines changed

15 files changed

+95
-44
lines changed

graalpython/com.oracle.graal.python.cext/setup.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@
4747
import _sysconfig
4848

4949
__dir__ = __file__.rpartition("/")[0]
50-
cflags_warnings = ["-Wno-int-to-pointer-cast", "-Wno-int-conversion", "-Wno-incompatible-pointer-types-discards-qualifiers", "-Wno-pointer-type-mismatch", "-Wno-braced-scalar-init"]
50+
cflags_warnings = [ "-Wno-int-to-pointer-cast"
51+
, "-Wno-int-conversion"
52+
, "-Wno-incompatible-pointer-types-discards-qualifiers"
53+
, "-Wno-pointer-type-mismatch"
54+
, "-Wno-braced-scalar-init"
55+
, "-Wno-deprecated-declarations"
56+
]
5157
libpython_name = "libpython"
5258
libhpy_name = "libhpy"
5359

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,11 @@ abstract static class DeflateCompress extends PythonTernaryBuiltinNode {
393393
Object deflateCompress(VirtualFrame frame, DeflaterWrapper stream, Object pb, int mode) {
394394
byte[] data = toBytes.execute(frame, pb);
395395
byte[] result = new byte[DEF_BUF_SIZE];
396-
397-
ByteArrayOutputStream baos = deflate(stream, mode, data, result);
398-
399-
return factory().createBytes(baos.toByteArray());
396+
return factory().createBytes(deflate(stream, mode, data, result));
400397
}
401398

402399
@TruffleBoundary
403-
private static ByteArrayOutputStream deflate(DeflaterWrapper stream, int mode, byte[] data, byte[] result) {
400+
private static byte[] deflate(DeflaterWrapper stream, int mode, byte[] data, byte[] result) {
404401
ByteArrayOutputStream baos = new ByteArrayOutputStream();
405402
stream.deflater.setInput(data);
406403
int deflateMode = mode;
@@ -418,7 +415,7 @@ private static ByteArrayOutputStream deflate(DeflaterWrapper stream, int mode, b
418415
if (mode == Z_FINISH) {
419416
stream.deflater.end();
420417
}
421-
return baos;
418+
return baos.toByteArray();
422419
}
423420
}
424421

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesBuiltins.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.nio.charset.CodingErrorAction;
5252
import java.util.ArrayList;
5353
import java.util.Arrays;
54+
import java.util.Iterator;
5455
import java.util.List;
5556

5657
import com.oracle.graal.python.PythonLanguage;
@@ -1401,12 +1402,28 @@ private int getIntValue(long from) {
14011402

14021403
private PList getBytesResult(List<byte[]> bytes) {
14031404
PList result = factory().createList();
1404-
for (byte[] bs : bytes) {
1405-
getAppendNode().execute(result, factory().createBytes(bs));
1405+
Iterator<byte[]> it = iterator(bytes);
1406+
while (hasNext(it)) {
1407+
getAppendNode().execute(result, factory().createBytes(next(it)));
14061408
}
14071409
return result;
14081410
}
14091411

1412+
@TruffleBoundary(allowInlining = true)
1413+
private static Iterator<byte[]> iterator(List<byte[]> bytes) {
1414+
return bytes.iterator();
1415+
}
1416+
1417+
@TruffleBoundary(allowInlining = true)
1418+
private static byte[] next(Iterator<byte[]> it) {
1419+
return it.next();
1420+
}
1421+
1422+
@TruffleBoundary(allowInlining = true)
1423+
private static boolean hasNext(Iterator<byte[]> it) {
1424+
return it.hasNext();
1425+
}
1426+
14101427
private PList getByteArrayResult(List<byte[]> bytes) {
14111428
PList result = factory().createList();
14121429
for (byte[] bs : bytes) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/InvalidateNativeObjectsAllManagedNode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -44,6 +44,7 @@
4444
import com.oracle.graal.python.builtins.objects.cext.InvalidateNativeObjectsAllManagedNodeFactory.InvalidateNativeObjectsAllManagedCachedNodeGen;
4545
import com.oracle.graal.python.nodes.PNodeWithContext;
4646
import com.oracle.truffle.api.Assumption;
47+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4748
import com.oracle.truffle.api.dsl.Cached;
4849
import com.oracle.truffle.api.dsl.Specialization;
4950

@@ -54,6 +55,7 @@ public abstract class InvalidateNativeObjectsAllManagedNode extends PNodeWithCon
5455
abstract static class InvalidateNativeObjectsAllManagedCachedNode extends InvalidateNativeObjectsAllManagedNode {
5556

5657
@Specialization(assumptions = {"singleContextAssumption()", "nativeObjectsAllManagedAssumption"})
58+
@TruffleBoundary
5759
void doValid(
5860
@Cached("nativeObjectsAllManagedAssumption()") Assumption nativeObjectsAllManagedAssumption) {
5961
nativeObjectsAllManagedAssumption.invalidate();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/DynamicObjectStorage.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static Object notStringLoop(DynamicObjectStorage self, Object key, ThreadState s
231231
@Exclusive @Cached("createBinaryProfile()") ConditionProfile gotState,
232232
@Exclusive @Cached("createBinaryProfile()") ConditionProfile noValueProfile) {
233233
long hash = self.getHashWithState(key, lib, state, gotState);
234-
Iterator<Object> keys = self.store.getShape().getKeys().iterator();
234+
Iterator<Object> keys = getKeysIterator(self.store.getShape());
235235
while (hasNext(keys)) {
236236
Object currentKey = getNext(keys);
237237
if (currentKey instanceof String) {
@@ -252,6 +252,11 @@ static Object notStringLoop(DynamicObjectStorage self, Object key, ThreadState s
252252
return null;
253253
}
254254

255+
@TruffleBoundary
256+
private static Iterator<Object> getKeysIterator(Shape shape) {
257+
return shape.getKeys().iterator();
258+
}
259+
255260
@TruffleBoundary
256261
private static Object getNext(Iterator<Object> keys) {
257262
return keys.next();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PFunction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public Object[] getDefaults() {
164164
return defaultValues;
165165
}
166166

167+
@TruffleBoundary
167168
public void setDefaults(Object[] defaults) {
168169
this.defaultsStableAssumption.invalidate("defaults changed for function " + getName());
169170
this.defaultValues = defaults;
@@ -173,6 +174,7 @@ public PKeyword[] getKwDefaults() {
173174
return kwDefaultValues;
174175
}
175176

177+
@TruffleBoundary
176178
public void setKwDefaults(PKeyword[] defaults) {
177179
this.defaultsStableAssumption.invalidate("kw defaults changed for function " + getName());
178180
this.kwDefaultValues = defaults;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2501,7 +2501,7 @@ static String doPInt(PInt self) {
25012501
@Specialization(limit = "1")
25022502
static String doNativeVoidPtr(PythonNativeVoidPtr self,
25032503
@CachedLibrary("self") PythonObjectLibrary lib) {
2504-
return Long.toString(lib.hash(self));
2504+
return doL(lib.hash(self));
25052505
}
25062506
}
25072507

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/PInt.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ private static String toString(BigInteger value) {
319319
return value.toString();
320320
}
321321

322+
@TruffleBoundary
323+
public static String toHexString(long value) {
324+
return Long.toHexString(value);
325+
}
326+
322327
@TruffleBoundary
323328
public static BigInteger longToBigInteger(long value) {
324329
return BigInteger.valueOf(value);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/mmap/MMapBuiltins.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -510,38 +510,35 @@ PBytes read(VirtualFrame frame, PMMap self, Object n,
510510
abstract static class ReadlineNode extends PythonUnaryBuiltinNode implements ByteReadingNode {
511511

512512
@Specialization
513-
Object readline(PMMap self,
514-
@Cached SequenceStorageNodes.AppendNode appendNode) {
515-
513+
Object readline(PMMap self) {
516514
try {
517-
ByteBuffer buf = ByteBuffer.allocate(4096);
518-
SeekableByteChannel channel = self.getChannel();
519-
ByteSequenceStorage res = new ByteSequenceStorage(16);
520-
// search for newline char
521-
outer: while (readIntoBuffer(channel, buf) > 0) {
522-
buf.flip();
523-
while (buf.hasRemaining()) {
524-
byte b = buf.get();
525-
// CPython really tests for '\n' only
526-
if (b != (byte) '\n') {
527-
appendNode.execute(res, b, BytesLikeNoGeneralizationNode.SUPPLIER);
528-
} else {
529-
// recover correct position (i.e. number of remaining bytes in buffer)
530-
PMMap.position(channel, PMMap.position(channel) - buf.remaining() - 1);
531-
break outer;
532-
}
533-
}
534-
buf.clear();
535-
}
536-
return factory().createBytes(res);
515+
return factory().createBytes(doReadline(self.getChannel()));
537516
} catch (IOException e) {
538517
throw raise(PythonBuiltinClassType.OSError, e);
539518
}
540519
}
541520

542-
@TruffleBoundary(allowInlining = true)
543-
private static int readIntoBuffer(SeekableByteChannel ch, ByteBuffer dst) throws IOException {
544-
return ch.read(dst);
521+
@TruffleBoundary
522+
static ByteSequenceStorage doReadline(SeekableByteChannel channel) throws IOException {
523+
ByteBuffer buf = ByteBuffer.allocate(4096);
524+
ByteSequenceStorage res = new ByteSequenceStorage(16);
525+
// search for newline char
526+
outer: while (channel.read(buf) > 0) {
527+
buf.flip();
528+
while (buf.hasRemaining()) {
529+
byte b = buf.get();
530+
// CPython really tests for '\n' only
531+
if (b != (byte) '\n') {
532+
SequenceStorageNodes.AppendNode.getUncached().execute(res, b, BytesLikeNoGeneralizationNode.SUPPLIER);
533+
} else {
534+
// recover correct position (i.e. number of remaining bytes in buffer)
535+
PMMap.position(channel, PMMap.position(channel) - buf.remaining() - 1);
536+
break outer;
537+
}
538+
}
539+
buf.clear();
540+
}
541+
return res;
545542
}
546543
}
547544

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket/SocketBuiltins.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ Object recvInto(VirtualFrame frame, PSocket socket, PMemoryView buffer, Object f
399399
@Cached("create(__SETITEM__)") LookupAndCallTernaryNode setItem) {
400400
int bufferLen = lib.asSizeWithState(callLen.executeObject(frame, buffer), PArguments.getThreadState(frame));
401401
byte[] targetBuffer = new byte[bufferLen];
402-
ByteBuffer byteBuffer = ByteBuffer.wrap(targetBuffer);
402+
ByteBuffer byteBuffer = wrap(targetBuffer);
403403
int length;
404404
try {
405405
length = fillBuffer(socket, byteBuffer);
@@ -436,7 +436,7 @@ Object recvInto(VirtualFrame frame, PSocket socket, PByteArray buffer, Object fl
436436
}
437437
} else {
438438
byte[] targetBuffer = new byte[bufferLen];
439-
ByteBuffer byteBuffer = ByteBuffer.wrap(targetBuffer);
439+
ByteBuffer byteBuffer = wrap(targetBuffer);
440440
int length;
441441
try {
442442
length = fillBuffer(socket, byteBuffer);
@@ -458,6 +458,11 @@ private static int fillBuffer(PSocket socket, ByteBuffer byteBuffer) throws IOEx
458458
SocketChannel nativeSocket = socket.getSocket();
459459
return nativeSocket.read(byteBuffer);
460460
}
461+
462+
@TruffleBoundary
463+
private static ByteBuffer wrap(byte[] data) {
464+
return ByteBuffer.wrap(data);
465+
}
461466
}
462467

463468
// recvmsg(bufsize[, ancbufsize[, flags]])

0 commit comments

Comments
 (0)