Skip to content

Commit e330ff4

Browse files
committed
HDFS-16084. Fix getJNIEnv crash due to incorrect state set to tls var
Signed-off-by: Kevin Cai <caixh.kevin@gmail.com>
1 parent b189ef8 commit e330ff4

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs-tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ add_library(native_mini_dfs
4242
add_executable(test_native_mini_dfs test_native_mini_dfs.c)
4343
target_link_libraries(test_native_mini_dfs native_mini_dfs ${JAVA_JVM_LIBRARY})
4444
add_test(test_test_native_mini_dfs test_native_mini_dfs)
45+
46+
add_executable(test_libhdfs_get_jni test_libhdfs_getjni.c)
47+
target_link_libraries(test_libhdfs_get_jni hdfs_static)
48+
add_test(test_test_libhdfs_get_jni test_libhdfs_get_jni)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include <jni.h>
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
#include <hdfs/hdfs.h>
23+
#include "expect.h"
24+
25+
// hook the jvm runtime function. expect always failure
26+
_JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetDefaultJavaVMInitArgs(void* args) {
27+
return 1;
28+
}
29+
30+
// hook the jvm runtime function. expect always failure
31+
_JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_CreateJavaVM(JavaVM** pvm, void** penv, void* args) {
32+
return 1;
33+
}
34+
35+
int main(int argc, char **argv) {
36+
hdfsFS lfs;
37+
// connect to nothing, should fail but not crash
38+
lfs = hdfsConnectNewInstance(NULL, 0);
39+
EXPECT_NULL(lfs);
40+
if(lfs) {
41+
fprintf(stderr, "Oops! Don't expect the fs to be created successful!");
42+
exit(1);
43+
}
44+
// try again, should fail but not crash
45+
lfs = hdfsConnectNewInstance(NULL, 0);
46+
EXPECT_NULL(lfs);
47+
if(lfs) {
48+
fprintf(stderr, "Oops! Don't expect the fs to be created successful!");
49+
exit(1);
50+
}
51+
return 0;
52+
}
53+
54+
/**
55+
* vim: ts=4: sw=4: et:
56+
*/

hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/jni_helper.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -818,26 +818,31 @@ JNIEnv* getJNIEnv(void)
818818
fprintf(stderr, "getJNIEnv: Unable to create ThreadLocalState\n");
819819
return NULL;
820820
}
821-
if (threadLocalStorageSet(state)) {
822-
mutexUnlock(&jvmMutex);
823-
goto fail;
824-
}
825-
THREAD_LOCAL_STORAGE_SET_QUICK(state);
826821

827822
state->env = getGlobalJNIEnv();
828-
mutexUnlock(&jvmMutex);
829-
830823
if (!state->env) {
824+
mutexUnlock(&jvmMutex);
831825
goto fail;
832826
}
833827

834828
jthrowable jthr = NULL;
835829
jthr = initCachedClasses(state->env);
836830
if (jthr) {
831+
mutexUnlock(&jvmMutex);
837832
printExceptionAndFree(state->env, jthr, PRINT_EXC_ALL,
838833
"initCachedClasses failed");
839834
goto fail;
840835
}
836+
837+
if (threadLocalStorageSet(state)) {
838+
mutexUnlock(&jvmMutex);
839+
goto fail;
840+
}
841+
842+
// set the TLS var only when the state passes all the checks
843+
THREAD_LOCAL_STORAGE_SET_QUICK(state);
844+
mutexUnlock(&jvmMutex);
845+
841846
return state->env;
842847

843848
fail:

0 commit comments

Comments
 (0)