google / j2objc

A Java to iOS Objective-C translation tool and runtime.
http://j2objc.org
Apache License 2.0
5.99k stars 968 forks source link

EXC_BAD_ACCESS in new_JavaSecuritySecureRandom_init #534

Closed ex3ndr closed 8 years ago

ex3ndr commented 9 years ago

Unfortunately in latest (0.9.7) release there is some bug that prevents me of creating SecureRandom.

This is my stack trace:

#0  0x04efa0b0 in objc_msgSend ()
#1  0x04a7f762 in -[NSString rangeOfString:options:range:locale:] ()
#2  0x04a7f733 in -[NSString rangeOfString:] ()
#3  0x009fa4d0 in -[NSString(Extensions) indexOf:] ()
#4  0x007cd22c in JavaSecurityProvider_removeFromPropertyServiceTableWithId_ ()
#5  0x007cd738 in -[JavaSecurityProvider putWithId:withId:] ()
#6  0x006d065f in ComGoogleJ2objcSecurityIosSecurityProvider_init ()
#7  0x006d05b0 in -[ComGoogleJ2objcSecurityIosSecurityProvider init] ()
#8  0x009aa8cc in -[IOSConcreteClass newInstance] ()
#9  0x00967352 in +[OrgApacheHarmonySecurityFortressServices initialize] ()
#10 0x04ee7461 in _class_initialize ()
#11 0x04eeffe5 in lookUpImpOrForward ()
#12 0x04eefe8d in _class_lookupMethodAndLoadCache3 ()
#13 0x04efa12f in objc_msgSend ()
#14 0x009675c9 in OrgApacheHarmonySecurityFortressServices_getSecureRandomService ()
#15 0x007d1578 in JavaSecuritySecureRandom_init ()
#16 0x007d2273 in new_JavaSecuritySecureRandom_init ()```
tomball commented 9 years ago

Do you have a test that shows this issue? Also, what is your default locale? I ask because this test works on my system. If it's related to locales, it may not on your system.

import java.security.*;

public final class GenerateId {

  public static void main (String... arguments) {
    try {
      SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
      String randomNum = new Integer(prng.nextInt()).toString();
      MessageDigest sha = MessageDigest.getInstance("SHA-1");
      byte[] result =  sha.digest(randomNum.getBytes());

      System.out.println("Random number: " + randomNum);
      System.out.println("Message digest: " + byteArrayToHex(result));
    } catch (NoSuchAlgorithmException ex) {
      System.err.println(ex);
    }
  }

  static String byteArrayToHex(byte[] a) {
    StringBuilder sb = new StringBuilder(a.length * 2);
    for(byte b : a) {
      sb.append(String.format("%02x", b & 0xff));
    }
    return sb.toString();
  }
} 

Output:

$ j2objc GenerateId.java 
translating GenerateId.java
Translated 1 file: 0 errors, 0 warnings
Translated 3 methods as functions
$ j2objcc GenerateId.m
$ ./a.out GenerateId
Random number: 8351987
Message digest: c56dd91563d8b0f2a84d48e3e38c8234a3dd5318
tomball commented 9 years ago

What system are you running this on? Mac command-line, simulator, ??? device, ...?

ex3ndr commented 9 years ago

I am compiling on Mac, running on simulator or mac. I am creating SecureRandom with default constructor. Locales are different.

ex3ndr commented 9 years ago

I try to use org.json library and produces something same - EXC_BAD_ACCESS. I used built-in library and manually copied to sources, but still same error.

ex3ndr commented 9 years ago

I don't know why, but it crashes in lines in JSONTokenizer in nextValue() method:

        StringBuilder sb = new StringBuilder();
        while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) { // << HERE
            sb.append(c);
            c = this.next();
        }
        this.back();
ex3ndr commented 9 years ago

Founded problem with JSON and created separated issue: #542 But it seems to be similar problem.

tomball commented 9 years ago

What JSON library are you having trouble with? I'd need the project link to proceed. Even better would be a test that shows the problem.

On Sun, May 24, 2015 at 7:29 PM Stepan Korshakov notifications@github.com wrote:

Founded problem with JSON and created separated issue: #542 https://github.com/google/j2objc/issues/542 But it seems to be similar problem.

— Reply to this email directly or view it on GitHub https://github.com/google/j2objc/issues/534#issuecomment-105095610.

tomball commented 9 years ago

I ask because the JSONTokenizer lines you included work for me.

ex3ndr commented 9 years ago

I solved this issue by switching to different approach of building objc code. Instead of adding generated code manually to my project, i now build static library with gnu make and then link it to my project. I got same errors during process when i occasionaly disabled ARC, i guess this issue may have same case.