Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Component,Origin,License,Copyright
main,slf4j,mit,QOS.ch
main,libddwaf,apache-2.0,DataDog
main,weak-lock-free,apache-2.0,Rafael Winterhalter
tests,junit,epl-1.0,various
tests,ant,apache-2.0,Apache Software Foundation
tests,groovy,apache-2.0,various
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ configurations {
def SLF4J_VERSION = '1.7.30'
dependencies {
implementation group: 'org.slf4j', name: 'slf4j-api', version: SLF4J_VERSION
implementation group: 'com.blogspot.mydailyjava', name: 'weak-lock-free', version: '0.17'

testRuntimeOnly group: 'org.slf4j', name: 'slf4j-simple', version: SLF4J_VERSION
testImplementation group: 'org.hamcrest', name: 'hamcrest-library', version: '2.2'
Expand Down
4 changes: 2 additions & 2 deletions dl_dbgsyms
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ function extract {
function main {
local readonly version=$1
dl_zip $version
extract $version linux_64_glibc/libsqreen_jni
extract $version linux_64/libddwaf
extract $version linux/x86_64/glibc/libsqreen_jni
extract $version linux/x86_64/libddwaf
}

main $1
4 changes: 2 additions & 2 deletions src/main/c/jni/io_sqreen_powerwaf_Additive.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/main/c/jni/io_sqreen_powerwaf_Powerwaf.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 40 additions & 17 deletions src/main/c/powerwaf_jni.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ jclass *number_cls = &number_longValue.class_glob;
static struct j_method _boolean_booleanValue;
static jclass *_boolean_cls = &_boolean_booleanValue.class_glob;

// MapIterableWithSize
static jclass _miws_cls;

static struct j_method _hashmap_init;
static struct j_method _map_put;
struct j_method map_entryset;
Expand Down Expand Up @@ -596,10 +599,10 @@ static jobject _run_rule_common(bool is_byte_buffer, JNIEnv *env, jclass clazz,
/*
* Class: io_sqreen_powerwaf_Powerwaf
* Method: runRules
* Signature: (Lio/sqreen/powerwaf/PowerwafHandle;Ljava/util/Map;Lio/sqreen/powerwaf/Powerwaf$Limits;)Lio/sqreen/powerwaf/Powerwaf$ResultWithData;
* Signature: (Lio/sqreen/powerwaf/PowerwafHandle;Ljava/util/Object;Lio/sqreen/powerwaf/Powerwaf$Limits;)Lio/sqreen/powerwaf/Powerwaf$ResultWithData;
*/
JNIEXPORT jobject JNICALL
Java_io_sqreen_powerwaf_Powerwaf_runRules__Lio_sqreen_powerwaf_PowerwafHandle_2Ljava_util_Map_2Lio_sqreen_powerwaf_Powerwaf_00024Limits_2Lio_sqreen_powerwaf_PowerwafMetrics_2(
Java_io_sqreen_powerwaf_Powerwaf_runRules__Lio_sqreen_powerwaf_PowerwafHandle_2Ljava_lang_Object_2Lio_sqreen_powerwaf_Powerwaf_00024Limits_2Lio_sqreen_powerwaf_PowerwafMetrics_2(
JNIEnv *env, jclass clazz, jobject handle_obj, jobject parameters,
jobject limits_obj, jobject metrics_obj)
{
Expand Down Expand Up @@ -811,10 +814,10 @@ static jobject _run_additive_common(JNIEnv *env, jobject this,
* Class: io_sqreen_powerwaf_Additive
* Method: runAdditive
* Signature:
* (Ljava/util/Map;Lio/sqreen/powerwaf/Powerwaf$Limits;)Lio/sqreen/powerwaf/Powerwaf$ResultWithData;
* (Ljava/lang/Object;Lio/sqreen/powerwaf/Powerwaf$Limits;)Lio/sqreen/powerwaf/Powerwaf$ResultWithData;
*/
JNIEXPORT jobject JNICALL
Java_io_sqreen_powerwaf_Additive_runAdditive__Ljava_util_Map_2Lio_sqreen_powerwaf_Powerwaf_00024Limits_2Lio_sqreen_powerwaf_PowerwafMetrics_2(
Java_io_sqreen_powerwaf_Additive_runAdditive__Ljava_lang_Object_2Lio_sqreen_powerwaf_Powerwaf_00024Limits_2Lio_sqreen_powerwaf_PowerwafMetrics_2(
JNIEnv *env, jobject this, jobject parameters, jobject limits_obj,
jobject metrics_obj)
{
Expand Down Expand Up @@ -1128,17 +1131,18 @@ static bool _cache_single_class_weak(JNIEnv *env,

static bool _cache_classes(JNIEnv *env)
{
return _cache_single_class_weak(env, "java/lang/RuntimeException",
&jcls_rte) &&
_cache_single_class_weak(env, "java/lang/IllegalArgumentException",
&jcls_iae) &&
_cache_single_class_weak(env, "java/lang/CharSequence",
&charSequence_cls) &&
_cache_single_class_weak(env, "java/nio/Buffer",
&buffer_cls) &&
_cache_single_class_weak(env, "java/nio/CharBuffer",
&charBuffer_cls) &&
_cache_single_class_weak(env, "java/lang/String", &string_cls);
return _cache_single_class_weak(env, "java/lang/RuntimeException",
&jcls_rte) &&
_cache_single_class_weak(env, "java/lang/IllegalArgumentException",
&jcls_iae) &&
_cache_single_class_weak(env, "java/lang/CharSequence",
&charSequence_cls) &&
_cache_single_class_weak(env, "java/nio/Buffer", &buffer_cls) &&
_cache_single_class_weak(env, "java/nio/CharBuffer",
&charBuffer_cls) &&
_cache_single_class_weak(env, "java/lang/String", &string_cls) &&
_cache_single_class_weak(
env, "io/sqreen/powerwaf/MapIterableWithSize", &_miws_cls);
}

static void _dispose_of_weak_classes(JNIEnv *env)
Expand All @@ -1154,6 +1158,7 @@ static void _dispose_of_weak_classes(JNIEnv *env)
DESTROY_CLASS_REF(charSequence_cls)
DESTROY_CLASS_REF(buffer_cls)
DESTROY_CLASS_REF(charBuffer_cls)
DESTROY_CLASS_REF(_miws_cls)
// leave jcls_rte for last in OnUnload; we might still need it
}

Expand Down Expand Up @@ -1487,6 +1492,10 @@ static ddwaf_object _convert_checked_ex(JNIEnv *env, bool use_bools,
JNI(DeleteLocalRef, clazz);
}

// shared between two branches
jobject entry_set = NULL;
jobject entry_set_it;

if (JNI(IsSameObject, obj, NULL)) {
// replace NULLs with empty maps.
// DDWAF_OBJ_NULL is actually invalid; it can't be added to containers
Expand Down Expand Up @@ -1526,6 +1535,18 @@ static ddwaf_object _convert_checked_ex(JNIEnv *env, bool use_bools,
goto error;
}
}
} else if (JNI(IsInstanceOf, obj, _miws_cls)) {
ddwaf_object_map(&result);
if (rec_level >= lims->max_depth) {
JAVA_LOG(DDWAF_LOG_DEBUG,
"Leaving map empty because max depth of %d "
"has been reached",
lims->max_depth);
goto early_return;
}
JAVA_CALL(entry_set_it, iterable_iterator, obj);
goto iterator_map_entry;

} else if (JNI(IsInstanceOf, obj, *map_cls)) {
ddwaf_object_map(&result); // can't fail
if (rec_level >= lims->max_depth) {
Expand All @@ -1536,10 +1557,10 @@ static ddwaf_object _convert_checked_ex(JNIEnv *env, bool use_bools,
goto early_return;
}

jobject entry_set, entry_set_it;
JAVA_CALL(entry_set, map_entryset, obj);
JAVA_CALL(entry_set_it, iterable_iterator, entry_set);

iterator_map_entry:
while (JNI(CallBooleanMethod, entry_set_it, iterator_hasNext.meth_id)) {
if (JNI(ExceptionCheck)) {
goto error;
Expand Down Expand Up @@ -1592,7 +1613,9 @@ static ddwaf_object _convert_checked_ex(JNIEnv *env, bool use_bools,
}

JNI(DeleteLocalRef, entry_set_it);
JNI(DeleteLocalRef, entry_set);
if (entry_set) {
JNI(DeleteLocalRef, entry_set);
}

} else if (JNI(IsInstanceOf, obj, *iterable_cls)) {
ddwaf_object_array(&result);
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/io/sqreen/powerwaf/Additive.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class Additive implements Closeable {
private static native long initAdditive(PowerwafHandle handle);

private native Powerwaf.ResultWithData runAdditive(
Map<String, Object> parameters, Powerwaf.Limits limits, PowerwafMetrics metrics) throws AbstractPowerwafException;
Object parameters, Powerwaf.Limits limits, PowerwafMetrics metrics) throws AbstractPowerwafException;

private native Powerwaf.ResultWithData runAdditive(
ByteBuffer firstPWArgsBuffer, Powerwaf.Limits limits, PowerwafMetrics metrics) throws AbstractPowerwafException;
Expand All @@ -70,9 +70,30 @@ private native Powerwaf.ResultWithData runAdditive(
* @return execution results
* @throws AbstractPowerwafException rethrow from native code, timeout or param serialization failure
*/
public Powerwaf.ResultWithData run(Map<String, Object> parameters,
public Powerwaf.ResultWithData run(Map<?, Object> parameters,
Powerwaf.Limits limits,
PowerwafMetrics metrics) throws AbstractPowerwafException {
return run((Object) parameters, limits, metrics);
}

/**
* Push params to PowerWAF with given limits
*
* @param parameters data to push to PowerWAF
* @param limits request execution limits
* @param metrics a metrics collector, or null
* @return execution results
* @throws AbstractPowerwafException rethrow from native code, timeout or param serialization failure
*/
public Powerwaf.ResultWithData run(MapIterableWithSize<?> parameters,
Powerwaf.Limits limits,
PowerwafMetrics metrics) throws AbstractPowerwafException {
return run((Object) parameters, limits, metrics);
}

private Powerwaf.ResultWithData run(Object parameters,
Powerwaf.Limits limits,
PowerwafMetrics metrics) throws AbstractPowerwafException {
if (limits == null) {
throw new IllegalArgumentException("limits must be provided");
}
Expand Down
Loading