BCrypt very slow in app engine application - java

I'm developing a GWT/app engine application, using Eclipse. The below problem occurred after upgrading to app engine 1.6.4 from 1.6.3. After the upgrade, I my application would not work at all. Unfortunately, I had deleted my old app engine plugins, so I was unable to rollback to 1.6.3. After 2 hours of banging my head against the wall, I decided to recreate my Eclipse project. The project worked again, except for the following anomaly:
I'm using BCrypt to implement one-way hash encoding of passwords. Before yesterday, this worked fine, with password encodes and checks occurring very fast -- probably in a few milliseconds. Now these operations take on the order of 2 minutes! Using the debugger, I paused the application to see if I could figure out what was going on. Each time I pause, I get a stack trace such as the following:
Thread [798744730#qtp-2080190228-3] (Suspended)
Class<T>.forName0(String, boolean, ClassLoader) line: not available [native method]
Class<T>.forName(String) line: 186
RuntimeHelper.checkRestricted(boolean, String, String, String) line: 63
Runtime.checkRestricted(boolean, String, String, String) line: 63
BCrypt.encipher(int[], int) line: 496
BCrypt.key(byte[]) line: 558
BCrypt.crypt_raw(byte[], byte[], int) line: 622
BCrypt.hashpw(String, String) line: 681
BCrypt.checkpw(String, String) line: 749
BCrypt.encipher() is as follows: (line 496 is shown below in a line comment)
private final void encipher(int lr[], int off) {
int i, n, l = lr[off], r = lr[off + 1];
l ^= P[0];
for (i = 0; i <= BLOWFISH_NUM_ROUNDS - 2;) {
// Feistel substitution on left word
n = S[(l >> 24) & 0xff];
n += S[0x100 | ((l >> 16) & 0xff)];
n ^= S[0x200 | ((l >> 8) & 0xff)];
n += S[0x300 | (l & 0xff)];
r ^= n ^ P[++i]; //*** LINE 496 *****
// Feistel substitution on right word
n = S[(r >> 24) & 0xff];
n += S[0x100 | ((r >> 16) & 0xff)];
n ^= S[0x200 | ((r >> 8) & 0xff)];
n += S[0x300 | (r & 0xff)];
l ^= n ^ P[++i];
}
lr[off] = r ^ P[BLOWFISH_NUM_ROUNDS + 1];
lr[off + 1] = l;
}
Depending on when I pause the debugger, different lines in BCrypt are the caller to Runtime.checkRestricted(), but it appears that Runtime.checkRestricted() is called continuously. Since this is called in embedded loops, I'm thinking that this is the cause. I then went on a hunt as to how to avoid this checkRestricted() call from happening. No luck.
I have a somewhat complicated application structure that contains three Google web applications (Eclipse projects). I'll call them:
Base
Store
App
where Store depends upon Base, and App depends upon both Store and Base. I use an Ant task to build the Base and Store projects into JAR files and copy them to the App/war/WEB-INF/lib folder.
Originally, I had BCrypt in its own Eclipse project and my ANT task would also JAR this and copy it to App/war/WEB-INF/lib. This was working fine for the past few months until now. To try to work around the current problem, I tried moving the BCrypt class (it contains only 1 class) directly into the Base project, with the same result, then into the Store project, again with the same result. Since my app currently calls BCrypt methods only from the Store project, I figured either of this might work. They did, functionally, but still taking 2 minutes to complete an encipher() call.
From the stack trace, Runtime or RuntimeHelper return Source Not Found when I click on them, and I can find nothing about them in Google searches.
Questions:
Why is every line in BCrypt subjected to a checkRestricted() call? This doesn't seem normal.
More importantly, any idea on how to fix this problem?
I don't know what to look at next. Any ideas would be very welcome, even if you don't know the ultimate solution.
Thanks very much.
Rick

I just created a ticket at the GAE code project: http://code.google.com/p/googleappengine/issues/detail?id=7277&thanks=7277&ts=1333530915
Maybe, we get an answer there as using BCrypt is also provided by an official tutorial: http://code.google.com/p/google-web-toolkit-incubator/wiki/LoginSecurityFAQ
Google fixed the bug and released a new version (1.6.4.1) today:
http://code.google.com/p/googleappengine/downloads/detail?name=appengine-java-sdk-1.6.4.1.zip

Yes. There is a regression in 1.6.4
See Is google app engine 1.6.4 slower in local? Comments there by Googlers working on the GAE SDK.

Related

Invalid Memory Access with 64 bit dll's on windows 10 only with java11

I have a java application that uses jna to call a 64 bit third party DLL to communicate with scanners (with native 64 bit drivers). I am receiving an invalid memory access error with the combination of windows 10 / java11 / jna that I do not receive with any other combination.
This code works fine on all the following without errors:
Windows 7 / 64 bit java 8 runtime
Windows 7 / 64 bit java 11 runtime
Windows 10 / 64 bit java 8 runtime
I am using latest and greatest jna (5.3.1) and have tried older versions as well, with the same result.
/* Open Scanner */
EZT.TWAIN_SetHideUI(true);
if (EZT.TWAIN_GetSourceList()) {
if (!EZT.TWAIN_OpenDefaultSource()) {
throw new RuntimeException("Cannot open scanner");
}
}
/* Retrieve scanner capabilities for resolution */
int hcon = EZT.TWAIN_Get(EZLibrary.ICAP_XRESOLUTION);
if (hcon != 0) {
int resolutions[] = new int[EZT.CONTAINER_ItemCount(hcon)];
...
}
.....
private interface EZLibrary extends Library {
int ICAP_XRESOLUTION = 4376;
void TWAIN_SetHideUI(boolean bHide);
boolean TWAIN_GetSourceList();
boolean TWAIN_OpenDefaultSource();
int TWAIN_Get(int uCap);
int CONTAINER_ItemCount(int hcon);
}
The call to "EZT.CONTAINER_ItemCount(hcon)" in this example returns an Invalid Memory Access Error. However, when this code is a part of my larger application and not this sample application, the same code sequence throws the Invalid Memory access error up higher on "EZT.TWAIN_OpenDefaultSource()".
So, in summary:
windows 7 / java 8 : as expected results
windows 7 / java 11: as expected results
windows 10 / java 8: as expected results
windows 10 / java 11:
java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokeInt(Native Method)
at com.sun.jna.Function.invoke(Function.java:426)
at com.sun.jna.Function.invoke(Function.java:361)
at com.sun.jna.Library$Handler.invoke(Library.java:265)
at com.sun.jna.Native$3.invoke(Native.java:1202)
at $Proxy0.CONTAINER_ItemCount(Unknown Source)
My question becomes is there an issue with JNA with win 10 / java 11 or is it something in the third party code, or something I'm doing wrong?
I did end up finding a solution for this.
I am not entirely sure why, but for 64 bit my JNA implementation needed:
Long TWAIN_Get(int uCap);
Long TWAIN_Acquire(int hwndApp);
With 32 bit it needed to be:
Integer TWAIN_Get(int uCap);
Integer TWAIN_Acquire(int hwndApp);
So to handle both I just use:
Number hcon = EZT.TWAIN_Get(EZTwainPro.ICAP_XRESOLUTION);
Doing this resolved the invalid memory access. I know it is because of the int returned was an unsigned int, but I am not entirely sure why the "long" implementation only works on 64 bit and not 32 bit runtimes. It has to be different in order to have both work. Also, the "long" implementation was only needed for win10, not win7, but it works for win7 too.

Why Build.VERSION.SDK_INT showing incorrect number?

I'm developing an android application and want to support android Q Beta.
So I tried android Q beta on my pixel 1.
But, when I want to check specific SDK version, it showing incorrect number.
I have read this and for android Q, SDK_INT should be 10000 (because it will similar with constant_value CUR_DEVELOPMENT).
I tried debugging to get SDK_INT number with this code :
CustomLog.v("Build", "build int : " + Build.VERSION.SDK_INT + " | Curr Dev : " + Build.VERSION_CODES.CUR_DEVELOPMENT + " | Build baseOS: " + Build.VERSION.BASE_OS);
CustomLog.v("Build", "preview sdk int : " + Build.VERSION.PREVIEW_SDK_INT + " | Code Nanme : " + Build.VERSION.CODENAME + " | Version Q : " + Build.VERSION_CODES.Q);
I expect the output Build.VERSION.SDK_INT to be 10000, but the actual output is 28, which is constant_value for android P.
This is the log in my logcat:
and this is information the devices:
That's normal - and it is gonna be this way until Q is officially released I guess. If you want to test against Q you could use
BuildCompat#isAtLeastQ()
As point out by CommonsWare, The real value (29) should start appearing around Q Beta 4 when the APIs are supposed to be finalized.

Modify core java libraries of Android source for use in apps

I want to make a little modification to the Android source to meet my requirement. Here is the requirement:
I want index all the objects in an Android app by adding one more public int field to the java.lang.Object class. Therefore, all the classes can inherit the newly added field because all of them are the subclasses of the Object class.
What I have done so far is modify the java.lang.Object class under <Android_source>/libcore/libart/src/main/java/java/lang folder and recompile the source.
I want to ask if I am doing the right thing. Can my Android app recognize this change (e.g. can a String object access the newly added field)?
Edit
After around 3 weeks try and error, I finally got the complete answer. I want to share this experience with others if anybody else want to modify the core java libraries of Android source (e.g., modifying Object.java and String.java etc.). Again, as mentioned by Michael, please note that such a modification may only be suitable for research or testing purpose.
The key challenge in making a successful modification (here 'successful' means the modified Android source can be built and run on emulators or real devices without any problem) is that some of the classes in the core java library have their C++ mirrors (located in <Android_source>/art/runtime/mirrors/). When modifying these java classes, you should also make the same modifications to their C++ mirrors. Otherwise, you could fail the build process because there are a bunch of checkings that you need to pass. Since I only add a new field to the Object.java, I will list some checkings (or requirements) I encountered below:
1.The size of an object instance = the size of its C++ mirror. For example, if I add a long field into Object.java, I should also add a uint64_t field to its C++ mirror to make their size equal.
2.Try to make the size of an object instance be the power of 2 (e.g., 2, 4, 8, 16, ...). For example, the size of the original Object.java is 8, therefore I add a long field to increase the size to 16. If I add an int field, the size becomes 12 and it can fail many checkings. I don't figure out the exact reason but I guess it has something to do with memory alignment.
3.Try to put primitive-type fields after non-primitive-type fields and primitive-type fields should be ordered by size. This means you should put reference-type fields in the front, followed by 8-byte-primitive-type fields, then 4-byte-primitive-type fields, then 2-byte-primitive-type fields, then 1-byte-primitive-type fields. Again, I guess the reason is memory alignment
That's all I done to meet my requirements. I am open to any discussions if you have any ideas about the purpose of these checkings (especially the 2ed and 3rd one)
New edit
More specifically, I did the following things:
Add a new field (e.g., public long tag;) in Object.java
Change static constexpr uint32_t kObjectHeaderSize = kUseBrooksReadBarrier ? 16 : 8; to static constexpr uint32_t kObjectHeaderSize = kUseBrooksReadBarrier ? 24 : 16; in Object.h
Add the following method in Object.h (Only on Android 7)
static MemberOffset TagOffset() {
return OFFSET_OF_OBJECT_MEMBER(Object, tag);
}
Add a new public field public: uint64_t tag; in Object.h
Change
#define MIRROR_OBJECT_CLASS_OFFSET 0
ADD_TEST_EQ(MIRROR_OBJECT_CLASS_OFFSET, art::mirror::Object::ClassOffset().Int32Value())
#define MIRROR_OBJECT_LOCK_WORD_OFFSET 4
ADD_TEST_EQ(MIRROR_OBJECT_LOCK_WORD_OFFSET, art::mirror::Object::MonitorOffset().Int32Value())
#if defined(USE_BROOKS_READ_BARRIER)
#define MIRROR_OBJECT_HEADER_SIZE 16
#else
#define MIRROR_OBJECT_HEADER_SIZE 8
to
#define MIRROR_OBJECT_CLASS_OFFSET 0
ADD_TEST_EQ(MIRROR_OBJECT_CLASS_OFFSET, art::mirror::Object::ClassOffset().Int32Value())
#define MIRROR_OBJECT_LOCK_WORD_OFFSET 4
ADD_TEST_EQ(MIRROR_OBJECT_LOCK_WORD_OFFSET, art::mirror::Object::MonitorOffset().Int32Value())
#define MIRROR_OBJECT_CLASS_TAG 8
ADD_TEST_EQ(MIRROR_OBJECT_CLASS_TAG, art::mirror::Object::TagOffset().Int32Value())
#if defined(USE_BROOKS_READ_BARRIER)
#define MIRROR_OBJECT_HEADER_SIZE 24
#else
#define MIRROR_OBJECT_HEADER_SIZE 16
in asm_support.h (Only on Android 7)
Add addOffset(OFFSETOF_MEMBER(mirror::Object, tag), "tag"); in class_linker_test.cc
Change
static_assert(kObjectHeaderSize == sizeof(mirror::HeapReference<mirror::Class>) +
sizeof(LockWord),
to
static_assert(kObjectHeaderSize == sizeof(mirror::HeapReference<mirror::Class>) +
sizeof(LockWord) + 8,
in art/runtime/gc/collector/concurrent_copying.cc
8 Change static constexpr size_t kFirstElementOffset = 12u; to static constexpr size_t kFirstElementOffset = 20u; in array.h
9 Change static constexpr size_t kObjectAlignmentShift = 3; to static constexpr size_t kObjectAlignmentShift = 4; in runtime_globals.h (Not done yet)
10 Change
static_assert(kObjectAlignment == 8, "Alignment check");
class PACKED(8) ImageHeader {
to
static_assert(kObjectAlignment == 16, "Alignment check");
class PACKED(16) ImageHeader {
in image.h (Not done yet)
11 Change static constexpr size_t kAlignment = 8; to static constexpr size_t kAlignment = 16; in gc::space::BumpPointerSpace (Not done yet)
12 Change #!/usr/bin/python to #!/usr/local/bin/python in device/generic/goldfish/tools/mk_combined_img.py (The value depends on your /bin/env python)(Only on Android 10)
13 Change
#define DCHECK_ALIGNED_PARAM(value, alignment) \
DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value)
to
#define DCHECK_ALIGNED_PARAM(value, alignment) \
DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value) << "," << alignment
in art/libartbase/base/bit_utils.h (for debug purpose)(Only for Android 11)
14 Change
DCHECK_ALIGNED_PARAM(remaining_space, object_class->GetObjectSize());
Object* end = dst + remaining_space / object_class->GetObjectSize();
to
DCHECK_ALIGNED_PARAM(remaining_space, kObjectAlignment);
Object* end = dst + remaining_space / kObjectAlignment;
in art/dex2oat/linker/image_writer.cc (Only for Android 11)
Change
memcpy(reinterpret_cast<uint8_t*>(to_ref) + kObjectHeaderSize,
reinterpret_cast<const uint8_t*>(from_ref) + kObjectHeaderSize,
obj_size - kObjectHeaderSize);
to
memcpy(reinterpret_cast<uint8_t*>(to_ref) + kObjectHeaderSize - 8,
reinterpret_cast<const uint8_t*>(from_ref) + kObjectHeaderSize - 8,
obj_size - kObjectHeaderSize + 8);
in concurrent_copying.cc (on Android 10)(reference)
Firstly, I'd like to state that I don't think this is a good idea and is probably overkill outside of research purposes. If you are modifying AOSP then the code you write will be dependent on the target device running that customised build of AOSP. However, it is still possible.
I'm assuming you already know how to compile and flash a custom AOSP build to a device. In order to write code that makes use of your new functionality, you'll also need to compile a custom SDK. This is so that Android Studio will know that your new method exists within Object, and can compile correctly against it. The full documentation can be found here, but essentially it boils down to:
. build/envsetup.sh
lunch sdk-eng
make sdk
When you have your SDK zip file, you'll need to unzip it to your SDK's platforms directory - it should now show up checked in your SDK manager. If you have given your SDK a custom platform ID then you should be able to use that in your build.gradle files.
Disclaimer: This advice is purely from memory, it's a lengthy process so I've not had time to double-check, and chances are there may be a couple of minor things I've missed. This should get you most of the way towards where you want to be though.

Eigen - Levenberg Marquardt algorithm: invalid template arguments on definition of permutation

I'm trying to use the Eigen implementation of Levenberg Marquardt algorithm on a Android application. In order to use Eigen, I'm using Android NDK and jni. I've already tested Eigen with simple calculations (like matrix creation and vector sums) and it works perfectly. However, when I tried to use the Levenberg Marquardt algorithm I got some errors on the LevenbergMarquardt.h file from Eigen's library.
First, here is my code. I based on this code:
Eigen::MatrixXd matrix(count, 3);
for (int i = 0; i < count; i++) {
Eigen::VectorXd t(3);
t << x[i], y[i], accuracy[i];
matrix.row(i) = t;
}
distance_functor functor(matrix, count);
Eigen::NumericalDiff<distance_functor> numDiff(functor);
Eigen::LevenbergMarquardt<Eigen::NumericalDiff<distance_functor>,double> lm(numDiff);
lm.parameters.maxfev = 2000;
lm.parameters.xtol = 1.49012e-08;
lm.parameters.ftol = 1.49012e-08;
lm.parameters.gtol = 0;
lm.parameters.epsfcn = 0;
Eigen::LevenbergMarquardtSpace::Status ret = lm.minimize(poseResult);
And those are the error that I got. The first two errors are in Eigen's library, and the last one is on the LevenbergMarquardt object creation. I've also included the respective line of code of the error, following the message:
Invalid template
arguments LevenbergMarquardt.h /jnimath/jni/unsupported/Eigen/src/LevenbergMarquardt line
121 Semantic Error
typedef PermutationMatrix<Dynamic,Dynamic> PermutationType;
Invalid template
arguments LevenbergMarquardt.h /jnimath/jni/unsupported/Eigen/src/NonLinearOptimization line
103 Semantic Error
PermutationMatrix<Dynamic,Dynamic> permutation;
Invalid template arguments test.cpp /jnimath/jni line 47 Semantic
Error
Eigen::LevenbergMarquardt,double> lm(numDiff);
The first two errors are really strange because there are some other typedefs using Dynamic and they are not throwing errors.
Also, I noticed that I got some symbol errors on the compilation, which are:
Symbol
'YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY'
could not be resolved Matrix.h /jnimath/jni/Eigen/src/Core line
277 Semantic Error
Symbol 'YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX' could not be
resolved Matrix.h /jnimath/jni/Eigen/src/Core line 224 Semantic Error
So, I have two questions:
Why am I getting errors on those lines?
Does anyone know how to fix that problem?
Thank you

J2ME error - the method nextInt() in the type Random is not applicable for the arguments (int)

I just started to learn J2ME and right from the bat I got this error when using Random class. Arg, it's so frustrating. Anyone got the same problem before? I already tried to restart Eclipse, write only the code for the random generator to isolate it but to no avail.
I'm using CLDC 1.1 and MIDP 2.1 by the way.
Seems you are pointing out the wrong CLDC and MIDP libraries. new Random().nextInt(x) exists in CLDC 1.1.
If you are using CLDC 1.0 you can create your own implementation of nextInt(int):
public static int random(Random r, int n) {
if ((n & -n) == n) // i.e., n is a power of 2
return (int)((n * (long)r.nextInt()) >> 31);
int bits, val;
do {
bits = r.nextInt();
val = bits % n;
} while(bits - val + (n-1) < 0);
return val;
}
(Same implementation as nextInt(int) in the CLDC 1.1.)
nextInt(int) is only available since CLDC 1.1, so you have to specify CLDC 1.1 instead of 1.0
if you specified cldc 1.1 an still get this error its most likely an error of eclipse. took me hours to find:
in Eclipse go to Windows -> Preferences -> Java ME -> Device Management -> choose your Default Device -> Edit -> Libraries -> There you have to remove the cldc_1.0.jar
I had the same problem in netbeans, and solved it.
Right click on your project, go to properties.
Go To Libraries & Resources under build
Add Library
scroll for JMUnit for CLDC11
click OK
and build. all problems solved.
I just left JMUnit for CLDC11 there. not really sure if I need it. maybe I'll test without it once I have made up for lost time.

Categories

Resources