Merge pull request #139 from dicej/master

don't throw UnknownHostException from InetAddress.getByName("0.0.0.0")
This commit is contained in:
Joshua Warner 2013-12-18 11:07:11 -08:00
commit fd5bd9d77e
9 changed files with 138 additions and 100 deletions

View File

@ -10,8 +10,8 @@
#include "jni.h"
#include "avian/machine.h"
#include "sockets.h"
#include "jni-util.h"
using namespace avian::classpath::sockets;
@ -88,7 +88,8 @@ Java_java_net_InetAddress_ipv4AddressForName(JNIEnv* e,
if (host) {
return ntohl(reinterpret_cast<in_addr*>(host->h_addr_list[0])->s_addr);
} else {
fprintf(stderr, "trouble %d\n", WSAGetLastError());
throwNew(e, "java/net/UnknownHostException", 0);
return 0;
}
#else
addrinfo hints;
@ -100,19 +101,20 @@ Java_java_net_InetAddress_ipv4AddressForName(JNIEnv* e,
int r = getaddrinfo(chars, 0, &hints, &result);
e->ReleaseStringUTFChars(name, chars);
jint address;
if (r != 0) {
address = 0;
throwNew(e, "java/net/UnknownHostException", 0);
return 0;
} else {
address = ntohl
int address = ntohl
(reinterpret_cast<sockaddr_in*>(result->ai_addr)->sin_addr.s_addr);
freeaddrinfo(result);
return address;
}
return address;
#endif
} else {
throwNew(e, "java/lang/OutOfMemoryError", 0);
return 0;
}
return 0;
}

View File

@ -19,9 +19,6 @@ public class InetAddress {
private InetAddress(String name) throws UnknownHostException {
this.name = name;
this.ip = ipv4AddressForName(name);
if (ip == 0) {
throw new UnknownHostException(name);
}
}
public String getHostName() {

View File

@ -286,6 +286,24 @@ public class Arrays {
return true;
}
public static boolean equals(byte[] a, byte[] b) {
if(a == b) {
return true;
}
if(a == null || b == null) {
return false;
}
if(a.length != b.length) {
return false;
}
for(int i = 0; i < a.length; i++) {
if(a[i] != b[i]) {
return false;
}
}
return true;
}
public static <T> List<T> asList(final T ... array) {
return new AbstractList<T>() {
public int size() {

View File

@ -48,6 +48,8 @@ public final class Unsafe {
public native void putIntVolatile(Object o, long offset, int x);
public native long getLongVolatile(Object o, long offset);
public native void putOrderedInt(Object o, long offset, int x);
public native Object getObject(Object o, long offset);

View File

@ -652,7 +652,8 @@ ifeq ($(platform),darwin)
sdk-dir = $(platform-dir)/Developer/SDKs
ios-version := $(shell \
if test -d $(sdk-dir)/$(target)6.1.sdk; then echo 6.1; \
if test -d $(sdk-dir)/$(target)7.0.sdk; then echo 7.0; \
elif test -d $(sdk-dir)/$(target)6.1.sdk; then echo 6.1; \
elif test -d $(sdk-dir)/$(target)6.0.sdk; then echo 6.0; \
elif test -d $(sdk-dir)/$(target)5.1.sdk; then echo 5.1; \
elif test -d $(sdk-dir)/$(target)5.0.sdk; then echo 5.0; \
@ -666,6 +667,12 @@ ifeq ($(platform),darwin)
ios-bin = $(platform-dir)/Developer/usr/bin
found-gcc = $(shell if test -f $(ios-bin)/gcc; then echo true; else echo false; fi)
ifeq ($(found-gcc),false)
use-clang = true
endif
ifeq ($(use-clang),true)
cxx = clang -std=c++11
cc = clang

View File

@ -19,8 +19,8 @@
# include "libkern/OSAtomic.h"
# include "libkern/OSCacheControl.h"
# include "mach/mach_types.h"
# include "mach/arm/thread_act.h"
# include "mach/arm/thread_status.h"
# include "mach/thread_act.h"
# include "mach/thread_status.h"
# define THREAD_STATE ARM_THREAD_STATE
# define THREAD_STATE_TYPE arm_thread_state_t

View File

@ -48,6 +48,60 @@ resolveSystemClassThrow(Thread* t, object loader, object spec)
(t, loader, spec, true, Machine::ClassNotFoundExceptionType);
}
object
fieldForOffsetInClass(Thread* t, object c, unsigned offset)
{
object super = classSuper(t, c);
if (super) {
object field = fieldForOffsetInClass(t, super, offset);
if (field) {
return field;
}
}
object table = classFieldTable(t, c);
if (table) {
for (unsigned i = 0; i < objectArrayLength(t, table); ++i) {
object field = objectArrayBody(t, table, i);
if ((fieldFlags(t, field) & ACC_STATIC) == 0
and fieldOffset(t, field) == offset)
{
return field;
}
}
}
return 0;
}
object
fieldForOffset(Thread* t, object o, unsigned offset)
{
object c = objectClass(t, o);
if (classVmFlags(t, c) & SingletonFlag) {
c = singletonObject(t, o, 0);
object table = classFieldTable(t, c);
if (table) {
for (unsigned i = 0; i < objectArrayLength(t, table); ++i) {
object field = objectArrayBody(t, table, i);
if ((fieldFlags(t, field) & ACC_STATIC)
and fieldOffset(t, field) == offset)
{
return field;
}
}
}
abort(t);
} else {
object field = fieldForOffsetInClass(t, c, offset);
if (field) {
return field;
} else {
abort(t);
}
}
}
} // namespace
extern "C" AVIAN_EXPORT void JNICALL
@ -694,7 +748,7 @@ Avian_sun_misc_Unsafe_compareAndSwapLong
return atomicCompareAndSwap64
(&fieldAtOffset<uint64_t>(target, offset), expect, update);
#else
ACQUIRE_FIELD_FOR_WRITE(t, local::fieldForOffset(t, target, offset));
ACQUIRE_FIELD_FOR_WRITE(t, fieldForOffset(t, target, offset));
if (fieldAtOffset<uint64_t>(target, offset) == expect) {
fieldAtOffset<uint64_t>(target, offset) = update;
return true;
@ -704,6 +758,36 @@ Avian_sun_misc_Unsafe_compareAndSwapLong
#endif
}
extern "C" AVIAN_EXPORT int64_t JNICALL
Avian_sun_misc_Unsafe_getLongVolatile
(Thread* t, object, uintptr_t* arguments)
{
object o = reinterpret_cast<object>(arguments[1]);
int64_t offset; memcpy(&offset, arguments + 2, 8);
// avoid blocking the VM if this is being called in a busy loop
PROTECT(t, o);
{ ENTER(t, Thread::IdleState); }
object field;
if (BytesPerWord < 8) {
field = fieldForOffset(t, o, offset);
PROTECT(t, field);
acquire(t, field);
}
int64_t result = fieldAtOffset<int64_t>(o, offset);
if (BytesPerWord < 8) {
release(t, field);
} else {
loadMemoryBarrier();
}
return result;
}
extern "C" AVIAN_EXPORT void JNICALL
Avian_sun_misc_Unsafe_unpark
(Thread* t, object, uintptr_t* arguments)

View File

@ -2460,60 +2460,6 @@ pipeAvailable(int fd, int* available)
#endif
}
object
fieldForOffsetInClass(Thread* t, object c, unsigned offset)
{
object super = classSuper(t, c);
if (super) {
object field = fieldForOffsetInClass(t, super, offset);
if (field) {
return field;
}
}
object table = classFieldTable(t, c);
if (table) {
for (unsigned i = 0; i < objectArrayLength(t, table); ++i) {
object field = objectArrayBody(t, table, i);
if ((fieldFlags(t, field) & ACC_STATIC) == 0
and fieldOffset(t, field) == offset)
{
return field;
}
}
}
return 0;
}
object
fieldForOffset(Thread* t, object o, unsigned offset)
{
object c = objectClass(t, o);
if (classVmFlags(t, c) & SingletonFlag) {
c = singletonObject(t, o, 0);
object table = classFieldTable(t, c);
if (table) {
for (unsigned i = 0; i < objectArrayLength(t, table); ++i) {
object field = objectArrayBody(t, table, i);
if ((fieldFlags(t, field) & ACC_STATIC)
and fieldOffset(t, field) == offset)
{
return field;
}
}
}
abort(t);
} else {
object field = fieldForOffsetInClass(t, c, offset);
if (field) {
return field;
} else {
abort(t);
}
}
}
} // namespace local
} // namespace
@ -2737,36 +2683,6 @@ Avian_sun_misc_Unsafe_getDouble__Ljava_lang_Object_2J
(t, method, arguments);
}
extern "C" AVIAN_EXPORT int64_t JNICALL
Avian_sun_misc_Unsafe_getLongVolatile
(Thread* t, object, uintptr_t* arguments)
{
object o = reinterpret_cast<object>(arguments[1]);
int64_t offset; memcpy(&offset, arguments + 2, 8);
// avoid blocking the VM if this is being called in a busy loop
PROTECT(t, o);
{ ENTER(t, Thread::IdleState); }
object field;
if (BytesPerWord < 8) {
field = local::fieldForOffset(t, o, offset);
PROTECT(t, field);
acquire(t, field);
}
int64_t result = fieldAtOffset<int64_t>(o, offset);
if (BytesPerWord < 8) {
release(t, field);
} else {
loadMemoryBarrier();
}
return result;
}
extern "C" AVIAN_EXPORT void JNICALL
Avian_sun_misc_Unsafe_putByte__Ljava_lang_Object_2JB
(Thread*, object, uintptr_t* arguments)

View File

@ -135,7 +135,7 @@ public class Misc {
}
}
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
zam();
Bim bim = new Baz();
@ -287,6 +287,18 @@ public class Misc {
} catch (IOException e) {
throw new RuntimeException(e);
}
expect(java.util.Arrays.equals
(new byte[] { 0, 0, 0, 0 },
java.net.InetAddress.getByName("0.0.0.0").getAddress()));
try {
java.net.InetAddress.getByName
("bs.thisdomaindoesntexistseriouslynoway");
throw new AssertionError();
} catch (java.net.UnknownHostException e) {
// cool
}
}
protected class Protected { }