Skip to content

Commit

Permalink
Handle allowed and disallowed null parameters. (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkrodrigues authored May 22, 2023
1 parent b94d952 commit 7ac7240
Show file tree
Hide file tree
Showing 12 changed files with 336 additions and 119 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
</properties>

<dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.0</version>
<scope>provided</scope>
</dependency>

<!-- Dependencies for testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ Java_com_yscope_clp_compressorfrontend_AbstractClpWildcardQueryEncoder_setVariab
JNIEnv* jni_env,
jobject,
jbyteArray Java_variablesSchemaVersion,
jbyteArray Java_variableEncodingMethodsVersion
jint variables_schema_version_len,
jbyteArray Java_variableEncodingMethodsVersion,
jint variable_encoding_methods_len
) {
LIBCLP_FFI_JAVA_EXCEPTION_CATCHALL_BEGIN()
libclp_ffi_java::validate_variable_handling_rule_versions(jni_env, Java_variablesSchemaVersion,
Java_variableEncodingMethodsVersion);
variables_schema_version_len,
Java_variableEncodingMethodsVersion,
variable_encoding_methods_len);
LIBCLP_FFI_JAVA_EXCEPTION_CATCHALL_END()
}
142 changes: 94 additions & 48 deletions src/main/cpp/libclp_ffi_java/Java_MessageDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,64 @@ static void batch_encoded_vars_wildcard_match_native (
* See MessageDecoder::decodeMessageNative in Java
* @param jni_env
* @param Java_logtype
* @param logtype_len
* @param Java_allDictionaryVars
* @param all_dictionary_vars_len
* @param Java_dictionaryVarEndOffsets
* @param dictionary_var_end_offsets_len
* @param Java_encodedVars
* @param encoded_vars_len
* @return The decoded message
*/
static jbyteArray decode_message_native (JNIEnv* jni_env, jbyteArray Java_logtype,
jbyteArray Java_allDictionaryVars,
jintArray Java_dictionaryVarEndOffsets,
jlongArray Java_encodedVars);
static jbyteArray decode_message_native (
JNIEnv* jni_env,
jbyteArray Java_logtype,
jint logtype_len,
jbyteArray Java_allDictionaryVars,
jint all_dictionary_vars_len,
jintArray Java_dictionaryVarEndOffsets,
jint dictionary_var_end_offsets_len,
jlongArray Java_encodedVars,
jint encoded_vars_len
);

/**
* Wrapper around wildcard_query_matches_any_encoded_var which
* @tparam var_placeholder
* @param jni_env
* @param Java_wildcard_query
* @param Java_wildcardQuery
* @param wildcard_query_len
* @param Java_logtype
* @param Java_encoded_vars
* @param logtype_len
* @param Java_encodedVars
* @param encoded_vars_len
* @return true if a match was found, false otherwise
*/
template <VariablePlaceholder var_placeholder>
static bool jni_wildcard_query_matches_any_encoded_var (
JNIEnv* jni_env, jbyteArray Java_wildcard_query, jbyteArray Java_logtype,
jlongArray Java_encoded_vars);
JNIEnv* jni_env,
jbyteArray Java_wildcardQuery,
jint wildcard_query_len,
jbyteArray Java_logtype,
jint logtype_len,
jlongArray Java_encodedVars,
jint encoded_vars_len
);

JNIEXPORT void JNICALL
Java_com_yscope_clp_compressorfrontend_MessageDecoder_setVariableHandlingRuleVersions (
JNIEnv* jni_env,
jobject,
jbyteArray Java_variablesSchemaVersion,
jbyteArray Java_variableEncodingMethodsVersion
jint variables_schema_version_len,
jbyteArray Java_variableEncodingMethodsVersion,
jint variable_encoding_methods_len
) {
LIBCLP_FFI_JAVA_EXCEPTION_CATCHALL_BEGIN()
libclp_ffi_java::validate_variable_handling_rule_versions(jni_env, Java_variablesSchemaVersion,
Java_variableEncodingMethodsVersion);
variables_schema_version_len,
Java_variableEncodingMethodsVersion,
variable_encoding_methods_len);
LIBCLP_FFI_JAVA_EXCEPTION_CATCHALL_END()
}

Expand All @@ -101,39 +125,55 @@ Java_com_yscope_clp_compressorfrontend_MessageDecoder_decodeMessageNative (
JNIEnv* jni_env,
jobject,
jbyteArray Java_logtype,
jint logtype_len,
jbyteArray Java_allDictionaryVars,
jint all_dictionary_vars_len,
jintArray Java_dictionaryVarEndOffsets,
jlongArray Java_encodedVars
jint dictionary_var_end_offsets_len,
jlongArray Java_encodedVars,
jint encoded_vars_len
) {
LIBCLP_FFI_JAVA_EXCEPTION_CATCHALL_BEGIN()
return decode_message_native(jni_env, Java_logtype, Java_allDictionaryVars,
Java_dictionaryVarEndOffsets, Java_encodedVars);
return decode_message_native(jni_env, Java_logtype, logtype_len, Java_allDictionaryVars,
all_dictionary_vars_len, Java_dictionaryVarEndOffsets,
dictionary_var_end_offsets_len, Java_encodedVars,
encoded_vars_len);
LIBCLP_FFI_JAVA_EXCEPTION_CATCHALL_END(nullptr)
}

JNIEXPORT jboolean JNICALL
Java_com_yscope_clp_compressorfrontend_MessageDecoder_wildcardQueryMatchesAnyFloatVarNative (
JNIEnv* jni_env,
jobject,
jbyteArray Java_wildcard_query,
jbyteArray Java_wildcardQuery,
jint wildcard_query_len,
jbyteArray Java_logtype,
jlongArray Java_encoded_vars
jint logtype_len,
jlongArray Java_encodedVars,
jint encoded_vars_len
) {
LIBCLP_FFI_JAVA_EXCEPTION_CATCHALL_BEGIN()
return jni_wildcard_query_matches_any_encoded_var<VariablePlaceholder::Float>(
jni_env, Java_wildcard_query, Java_logtype, Java_encoded_vars);
jni_env, Java_wildcardQuery, wildcard_query_len, Java_logtype, logtype_len,
Java_encodedVars, encoded_vars_len);
LIBCLP_FFI_JAVA_EXCEPTION_CATCHALL_END(false)
}

JNIEXPORT jboolean JNICALL
Java_com_yscope_clp_compressorfrontend_MessageDecoder_wildcardQueryMatchesAnyIntVarNative (
JNIEnv* jni_env,
jobject,
jbyteArray Java_wildcard_query,
jbyteArray Java_wildcardQuery,
jint wildcard_query_len,
jbyteArray Java_logtype,
jlongArray Java_encoded_vars
jint logtype_len,
jlongArray Java_encodedVars,
jint encoded_vars_len
) {
LIBCLP_FFI_JAVA_EXCEPTION_CATCHALL_BEGIN()
return jni_wildcard_query_matches_any_encoded_var<VariablePlaceholder::Integer>(
jni_env, Java_wildcard_query, Java_logtype, Java_encoded_vars);
jni_env, Java_wildcardQuery, wildcard_query_len, Java_logtype, logtype_len,
Java_encodedVars, encoded_vars_len);
LIBCLP_FFI_JAVA_EXCEPTION_CATCHALL_END(false)
}

Expand Down Expand Up @@ -245,12 +285,12 @@ static void batch_encoded_vars_wildcard_match_native (
throw JavaExceptionOccurred(ErrorCode_Failure, __FILENAME__, __LINE__,
"GetObjectArrayElement failed");
}
auto logtype_len = jni_env->GetArrayLength(Java_logtype);
auto logtype_len = nullptr == Java_logtype ? 0 : jni_env->GetArrayLength(Java_logtype);
auto logtype_bytes = get_java_primitive_array_elements<jbyteArray, jbyte>(jni_env,
Java_logtype,
JNI_ABORT);
auto logtype = string_view{size_checked_pointer_cast<char>(logtype_bytes.get()),
static_cast<size_t>(logtype_len)};
string_view logtype{size_checked_pointer_cast<char>(logtype_bytes.get()),
static_cast<size_t>(logtype_len)};

try {
match_results[i] = wildcard_match_encoded_vars(
Expand All @@ -270,42 +310,45 @@ static void batch_encoded_vars_wildcard_match_native (
}
}

static jbyteArray decode_message_native (JNIEnv* jni_env, jbyteArray Java_logtype,
jbyteArray Java_allDictionaryVars,
jintArray Java_dictionaryVarEndOffsets,
jlongArray Java_encodedVars)
{
static jbyteArray decode_message_native (
JNIEnv* jni_env,
jbyteArray Java_logtype,
jint logtype_len,
jbyteArray Java_allDictionaryVars,
jint all_dictionary_vars_len,
jintArray Java_dictionaryVarEndOffsets,
jint dictionary_var_end_offsets_len,
jlongArray Java_encodedVars,
jint encoded_vars_len
) {
// Get logtype
auto logtype_bytes = get_java_primitive_array_elements<jbyteArray, jbyte>(jni_env, Java_logtype,
JNI_ABORT);
auto logtype_length = jni_env->GetArrayLength(Java_logtype);
string_view logtype(size_checked_pointer_cast<char>(logtype_bytes.get()), logtype_length);
string_view logtype{size_checked_pointer_cast<char>(logtype_bytes.get()),
static_cast<size_t>(logtype_len)};

// Get dictionary variables
auto all_dictionary_vars_bytes = get_java_primitive_array_elements<jbyteArray, jbyte>(
jni_env, Java_allDictionaryVars, JNI_ABORT);
auto all_dictionary_vars_length = jni_env->GetArrayLength(Java_allDictionaryVars);
string_view all_dictionary_vars(
string_view all_dictionary_vars{
size_checked_pointer_cast<char>(all_dictionary_vars_bytes.get()),
all_dictionary_vars_length);
static_cast<size_t>(all_dictionary_vars_len)};

auto dictionary_var_end_offsets = get_java_primitive_array_elements<jintArray, jint>(
jni_env, Java_dictionaryVarEndOffsets, JNI_ABORT);
auto dictionary_var_end_offsets_length = jni_env->GetArrayLength(Java_dictionaryVarEndOffsets);

// Get encoded variables
auto encoded_vars = get_java_primitive_array_elements<jlongArray, jlong>(
jni_env, Java_encodedVars, JNI_ABORT);
auto encoded_vars_length = jni_env->GetArrayLength(Java_encodedVars);

try {
auto message = decode_message(
logtype,
size_checked_pointer_cast<eight_byte_encoded_variable_t>(encoded_vars.get()),
encoded_vars_length,
encoded_vars_len,
all_dictionary_vars,
dictionary_var_end_offsets.get(),
dictionary_var_end_offsets_length
dictionary_var_end_offsets_len
);

auto Java_message = libclp_ffi_java::new_java_primitive_array<jbyteArray, jbyte>(
Expand All @@ -318,32 +361,35 @@ static jbyteArray decode_message_native (JNIEnv* jni_env, jbyteArray Java_logtyp

template <VariablePlaceholder var_placeholder>
static bool jni_wildcard_query_matches_any_encoded_var (
JNIEnv* jni_env, jbyteArray Java_wildcard_query, jbyteArray Java_logtype,
jlongArray Java_encoded_vars)
{
JNIEnv* jni_env,
jbyteArray Java_wildcardQuery,
jint wildcard_query_len,
jbyteArray Java_logtype,
jint logtype_len,
jlongArray Java_encodedVars,
jint encoded_vars_len
) {
// Get logtype
auto logtype_bytes = get_java_primitive_array_elements<jbyteArray, jbyte>(
jni_env, Java_logtype, JNI_ABORT);
auto logtype_length = jni_env->GetArrayLength(Java_logtype);
string_view logtype(size_checked_pointer_cast<char>(logtype_bytes.get()), logtype_length);
string_view logtype{size_checked_pointer_cast<char>(logtype_bytes.get()),
static_cast<size_t>(logtype_len)};

// Get wildcard query
auto wildcard_query_bytes = get_java_primitive_array_elements<jbyteArray, jbyte>(
jni_env, Java_wildcard_query, JNI_ABORT);
auto wildcard_query_length = jni_env->GetArrayLength(Java_wildcard_query);
string_view wildcard_query(size_checked_pointer_cast<char>(wildcard_query_bytes.get()),
wildcard_query_length);
jni_env, Java_wildcardQuery, JNI_ABORT);
string_view wildcard_query{size_checked_pointer_cast<char>(wildcard_query_bytes.get()),
static_cast<size_t>(wildcard_query_len)};

// Get encoded variables
auto encoded_vars = get_java_primitive_array_elements<jlongArray, jlong>(
jni_env, Java_encoded_vars, JNI_ABORT);
auto encoded_vars_length = jni_env->GetArrayLength(Java_encoded_vars);
jni_env, Java_encodedVars, JNI_ABORT);

try {
return wildcard_query_matches_any_encoded_var<var_placeholder>(
wildcard_query, logtype,
size_checked_pointer_cast<eight_byte_encoded_variable_t>(encoded_vars.get()),
encoded_vars_length
encoded_vars_len
);
} catch (const ffi::EncodingException& e) {
throw JavaIOException(__FILENAME__, __LINE__, jni_env, e.what());
Expand Down
8 changes: 6 additions & 2 deletions src/main/cpp/libclp_ffi_java/Java_MessageEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ Java_com_yscope_clp_compressorfrontend_MessageEncoder_setVariableHandlingRuleVer
JNIEnv* jni_env,
jobject,
jbyteArray Java_variablesSchemaVersion,
jbyteArray Java_variableEncodingMethodsVersion
jint variables_schema_version_len,
jbyteArray Java_variableEncodingMethodsVersion,
jint variable_encoding_methods_len
) {
LIBCLP_FFI_JAVA_EXCEPTION_CATCHALL_BEGIN()
libclp_ffi_java::validate_variable_handling_rule_versions(jni_env, Java_variablesSchemaVersion,
Java_variableEncodingMethodsVersion);
variables_schema_version_len,
Java_variableEncodingMethodsVersion,
variable_encoding_methods_len);
LIBCLP_FFI_JAVA_EXCEPTION_CATCHALL_END()
}

Expand Down
11 changes: 5 additions & 6 deletions src/main/cpp/libclp_ffi_java/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ namespace libclp_ffi_java {
void validate_variable_handling_rule_versions (
JNIEnv* jni_env,
jbyteArray Java_variablesSchemaVersion,
jbyteArray Java_variableEncodingMethodsVersion
jint variables_schema_version_len,
jbyteArray Java_variableEncodingMethodsVersion,
jint variable_encoding_methods_len
) {
// Validate the schemas version
auto schema_version_bytes = get_java_primitive_array_elements<jbyteArray, jbyte>(
jni_env, Java_variablesSchemaVersion, JNI_ABORT);
auto schema_version_bytes_length = jni_env->GetArrayLength(Java_variablesSchemaVersion);
string_view schema_version(size_checked_pointer_cast<char>(schema_version_bytes.get()),
schema_version_bytes_length);
variables_schema_version_len);
if (cVariablesSchemaVersion != schema_version) {
throw JavaUnsupportedOperationException(__FILENAME__, __LINE__, jni_env,
"Unsupported version for variables schema");
Expand All @@ -46,11 +47,9 @@ namespace libclp_ffi_java {
// Validate the encoding methods version
auto encoding_methods_version_bytes = get_java_primitive_array_elements<jbyteArray, jbyte>(
jni_env, Java_variableEncodingMethodsVersion, JNI_ABORT);
auto encoding_methods_version_length =
jni_env->GetArrayLength(Java_variableEncodingMethodsVersion);
string_view encoding_methods_version(
size_checked_pointer_cast<char>(encoding_methods_version_bytes.get()),
encoding_methods_version_length);
variable_encoding_methods_len);
if (cVariableEncodingMethodsVersion != encoding_methods_version) {
throw JavaUnsupportedOperationException(__FILENAME__, __LINE__, jni_env,
"Unsupported version for variable encoding"
Expand Down
9 changes: 7 additions & 2 deletions src/main/cpp/libclp_ffi_java/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ namespace libclp_ffi_java {
* @param Java_array The Java primitive array
* @param release_mode The JNI primitive array release mode (see the
* docs for JNIEnv::Release<Primitive>ArrayElements)
* @return A unique pointer to the native version of the array
* @return A unique pointer to the native version of the array, or a nullptr
* if the Java array pointer is null
* @throw libclp_ffi_java::JavaExceptionOccurred if we couldn't get the
* array due to a Java exception.
* @throw libclp_ffi_java::GeneralException if we couldn't get the array
Expand Down Expand Up @@ -87,12 +88,16 @@ namespace libclp_ffi_java {
* library.
* @param jni_env
* @param Java_variablesSchemaVersion
* @param variables_schema_version_len
* @param Java_variableEncodingMethodsVersion
* @param variable_encoding_methods_len
*/
void validate_variable_handling_rule_versions (
JNIEnv* jni_env,
jbyteArray Java_variablesSchemaVersion,
jbyteArray Java_variableEncodingMethodsVersion
jint variables_schema_version_len,
jbyteArray Java_variableEncodingMethodsVersion,
jint variable_encoding_methods_len
);
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/cpp/libclp_ffi_java/common.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ namespace libclp_ffi_java {
using DeleterType =
JavaPrimitiveArrayElementsDeleter<JavaArrayType, NativeArrayElementType>;
auto array = std::unique_ptr<NativeArrayElementType, DeleterType>{
get_array(jni_env, Java_array, nullptr),
nullptr == Java_array ? nullptr : get_array(jni_env, Java_array, nullptr),
DeleterType{jni_env, Java_array, release_mode}};
if (nullptr == Java_array) {
return array;
}
auto exception = jni_env->ExceptionOccurred();
if (nullptr != exception) {
throw JavaExceptionOccurred(ErrorCode_Failure, __FILENAME__, __LINE__,
Expand Down
Loading

0 comments on commit 7ac7240

Please sign in to comment.