[foreign] RFR 8212987 : Binder should allows negative values for native pointers

Jorn Vernee jbvernee at xs4all.nl
Tue Nov 6 21:21:30 UTC 2018


> Since you override checkBound and checkRange, negative address is
> possible. What I am suggesting is basically don’t override the length
> method, and that any pointer base on EVERYTHING is simply have a
> Long.MAX_VALUE of address space to access and be consistent all around
> until we figure out how to deal with length on MemoryRegion.

The problem then is that you don't know where to set the start of the 
region so that it covers everything that's needed for a native pointers. 
Like you said, there needs to be a way to 'look back' with a native 
pointer, so setting the start of the region to the pointer's offset 
won't work for that case. We could do something like have the pointer 
offset in the middle of the region, but that seems kind of hacky.

The weird thing right now is that BoundedMemoryRegion.Everything passes 
a length to the super constructor, but it is not actually used. I can 
improve that picture by refactoring into a MemoryRegion base class, 
which doesn't have a length and just does the READ/WRITE access checks, 
and then have BoundedMemoryRegion and `Everything` as a subclass of that 
(and maybe have a subclass for a ByteBuffer region as well). Then the 
`Everything` region won't have to use a length at all, and that should 
give an all-round consistency. Some memory regions will be sized, and 
some not, in particular: the memory regions of native pointers will not 
be sized, since there is no automatic way to know that size, but you 
could size the region explicitly by converting the pointer to an Array 
using Pointer::withSize, which will use BoundedMemoryRegion under the 
hood. And for memory allocated on the Java side, we can tell the size of 
the region automatically, so we can use BoundedMemoryRegion from the 
start.

I expected to remove the length() method with the other patch I linked, 
and then a MemoryRegion's length will just be an implementation detail. 
Moving the bytesSize() and elements() methods from Pointer to Array 
actually simplified most use cases since they were already using an 
array; `array.elementPointer().bytesSize()` -> `array.bytesSize()`.

What do you think?

Thanks,
Jorn


More information about the panama-dev mailing list