Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if data size exceeds java array vm limit when it is copied in jni #3850

Closed
wants to merge 4 commits into from

Conversation

azagrebin
Copy link
Contributor

to address issue #3849

code and throw RocksDBException in case of failure

const jsize jlen = static_cast<jsize>(size);
jbyteArray jbytes = env->NewByteArray(jlen);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you missing the check for OutOfMemoryError here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, updated

@facebook-github-bot
Copy link
Contributor

@azagrebin has updated the pull request. View: changes

@facebook-github-bot
Copy link
Contributor

@azagrebin has updated the pull request. View: changes

@azagrebin
Copy link
Contributor Author

not sure why travis fails (Error 137), maybe creating of 2.2Gb value is too big for it or the unit test takes too long

static jbyteArray copyBytes(JNIEnv* env, const Slice& bytes) {
const jsize jlen = static_cast<jsize>(bytes.size());

static jbyteArray createJavaByteArrayWithSizeCheck(JNIEnv* env, const char* bytes, const size_t size) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some function documentation please?
In particular can you add an @throws.

I have been using the Javadoc style even though it is C++, as there are some C++ documenters that will happily read javadoc style comments from C++.

// Integer.MAX_VALUE - 8 should be safe enough
static const size_t MAX_JARRAY_SIZE = ((static_cast<size_t>(1)) << 31) - 9;
if(size > MAX_JARRAY_SIZE) {
rocksdb::RocksDBExceptionJni::ThrowNew(env, "Requested array size exceeds VM limit");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The though occurs to me that, we are now potentially throwing a RocksDBException for any caller of copyBytes which we were not previously.

So we need to look for each Java function that eventually calls this code and make sure it has an @throws RocksDBException on both itself and its native interface function. A quick grep -r "copyBytes" java/rocksdbjni shows that we will in the least need changes to WriteBatch.Handler functions, amongst probably others.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, WriteBatch.Handler methods are called from JNI code which is behind WriteBatch.iterate (marked as throws RocksDBException). copyBytes is called from there before, to prepare key/value for WriteBatch.Handler. WriteBatch.Handler methods are user specific java code.

I also found couple of other places which can reach copyBytes.

@adamretter
Copy link
Collaborator

@azagrebin Regards your Travis problem, it seems you ran out of "resources", see: travis-ci/travis-ci#4665

This document (https://docs.travis-ci.com/user/reference/overview/) seems to suggest that we have 4GB available to the build, but I am not sure how much will be used up by Rocks internally etc.

I think for your JUnit test, we need to add an assumption (https://junit.org/junit4/javadoc/4.12/org/junit/Assume.html) that we have enough memory present to run your test. You could calculate the available memory (https://stackoverflow.com/questions/6220790/whats-the-best-way-to-calculate-available-memory-in-java#16517624) and check you have enough for your array in an Assumption.

We could also add a test in C++ to call the createJavaByteArrayWithSizeCheck function with some specific sizes, and check that it sets an exception. We are using Google's GTest framework. Is such a test something you might want to add?

@facebook-github-bot
Copy link
Contributor

@azagrebin has updated the pull request. View: changes

@azagrebin
Copy link
Contributor Author

@adamretter
the JNI code seems to have only java tests, as I understand the c++ tests are setup only for main db code (in make etc).

There is an option to expose test JNI method which calls createJavaByteArrayWithSizeCheck and test Exception from Java but not sure about this idea.

@azagrebin
Copy link
Contributor Author

@adamretter
do you think java test is enough after last changes?
or we need to setup GTest for jni c++ code or there is a better way to add it?

@azagrebin
Copy link
Contributor Author

@adamretter
can this PR be merged or C++ test or something else still needs to be added?

@adamretter
Copy link
Collaborator

@azagrebin yes I think it should be okay to merge. @sagar0 / @gfosco Can you merge please?

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riversand963 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sagar0 is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

sagar0 pushed a commit that referenced this pull request Jun 30, 2018
…ni (#3850)

Summary:
to address issue #3849
Closes #3850

Differential Revision: D8695487

Pulled By: sagar0

fbshipit-source-id: 04baeb2127663934ed1321fe6d9a9ec23c86e16b
rcane pushed a commit to rcane/rocksdb that referenced this pull request Sep 13, 2018
…ni (facebook#3850)

Summary:
to address issue facebook#3849
Closes facebook#3850

Differential Revision: D8695487

Pulled By: sagar0

fbshipit-source-id: 04baeb2127663934ed1321fe6d9a9ec23c86e16b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants