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

Make it possible to allow/disallow methods by their descriptor #48

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
*/
class AllowancesByteBuddyTransformer implements AgentBuilder.Transformer {

private Map<String, Map<String, Boolean>> allowances;
private Map<String, Map<String, Map<String, Boolean>>> allowances;

AllowancesByteBuddyTransformer(Map<String, Map<String, Boolean>> allowances) {
AllowancesByteBuddyTransformer(Map<String, Map<String, Map<String, Boolean>>> allowances) {
this.allowances = allowances;
}

Expand All @@ -50,7 +50,7 @@ public DynamicType.Builder<?> transform(
ClassLoader classLoader,
JavaModule module
) {
Map<String, Boolean> methods = allowances.get(typeDescription.getName());
Map<String, Map<String, Boolean>> methods = allowances.get(typeDescription.getName());

if (methods == null) {
return builder;
Expand All @@ -60,7 +60,14 @@ public DynamicType.Builder<?> transform(
.withCustomMapping()
.bind(new AllowedArgument.Factory(methods))
.to(AllowAdvice.class)
.on(method -> methods.containsKey(method.getName()));
.on(method -> {
Map<String, Boolean> byDescriptor = methods.get(method.getName());
if (byDescriptor == null) {
return false;
}

return byDescriptor.containsKey("*") || byDescriptor.containsKey(method.getDescriptor());
});

return builder.visit(advice);
}
Expand All @@ -76,9 +83,9 @@ public DynamicType.Builder<?> transform(
*/
class Factory implements Advice.OffsetMapping.Factory<AllowedArgument> {

final Map<String, Boolean> methods;
final Map<String, Map<String, Boolean>> methods;

Factory(Map<String, Boolean> methods) {
Factory(Map<String, Map<String, Boolean>> methods) {
this.methods = methods;
}

Expand All @@ -94,7 +101,12 @@ public Advice.OffsetMapping make(
AdviceType adviceType
) {
return (instrumentedType, instrumentedMethod, assigner, argumentHandler, sort) -> {
boolean allowed = methods.get(instrumentedMethod.getName());
Map<String, Boolean> byDescriptor = methods.get(instrumentedMethod.getName());

Boolean allowed = byDescriptor.get(instrumentedMethod.getDescriptor());
if (allowed == null) {
allowed = byDescriptor.get("*");
}
return Advice.OffsetMapping.Target.ForStackManipulation.of(allowed);
};
}
Expand Down
76 changes: 61 additions & 15 deletions agent/src/main/java/reactor/blockhound/BlockHound.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,28 @@ public static class Builder {
}
}};

private final Map<String, Map<String, Boolean>> allowances = new HashMap<String, Map<String, Boolean>>() {{
put(ClassLoader.class.getName(), new HashMap<String, Boolean>() {{
put("loadClass", true);
private final Map<String, Map<String, Map<String, Boolean>>> allowances = new HashMap<String, Map<String, Map<String, Boolean>>>() {{
put(ClassLoader.class.getName(), new HashMap<String, Map<String, Boolean>>() {{
put("loadClass", new HashMap<String, Boolean>() {{
put("(Ljava/lang/String;)Ljava/lang/Class;", true);
}});
}});
put(Throwable.class.getName(), new HashMap<String, Boolean>() {{
put("printStackTrace", true);
put(Throwable.class.getName(), new HashMap<String, Map<String, Boolean>>() {{
put("printStackTrace", new HashMap<String, Boolean>() {{
put("*", true);
}});
}});

put(ConcurrentHashMap.class.getName(), new HashMap<String, Boolean>() {{
put("initTable", true);
put(ConcurrentHashMap.class.getName(), new HashMap<String, Map<String, Boolean>>() {{
put("initTable", new HashMap<String, Boolean>() {{
put("*", true);
}});
}});

put(Advice.class.getName(), new HashMap<String, Boolean>() {{
put("to", true);
put(Advice.class.getName(), new HashMap<String, Map<String, Boolean>>() {{
put("to", new HashMap<String, Boolean>() {{
put("*", true);
}});
}});
}};

Expand All @@ -169,24 +177,62 @@ public static class Builder {

private Predicate<Thread> threadPredicate = t -> false;

public Builder markAsBlocking(Class clazz, String methodName, String signature) {
return markAsBlocking(clazz.getName(), methodName, signature);
public Builder markAsBlocking(Class clazz, String methodName, String descriptor) {
return markAsBlocking(clazz.getName(), methodName, descriptor);
}

public Builder markAsBlocking(String className, String methodName, String signature) {
public Builder markAsBlocking(String className, String methodName, String descriptor) {
blockingMethods.computeIfAbsent(className.replace(".", "/"), __ -> new HashMap<>())
.computeIfAbsent(methodName, __ -> new HashSet<>())
.add(signature);
.add(descriptor);
return this;
}

public Builder allowBlockingCallsInside(String className, String methodName) {
allowances.computeIfAbsent(className, __ -> new HashMap<>()).put(methodName, true);
return allowBlockingCallsInside(className, methodName, "*");
}

/**
* Allows blocking calls inside a method of a class with name identified by the provided className
* and that matches both provided methodName and descriptor.
*
* The descriptor should be in JVM's format:
* https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp276
*
* @param className class' name (e.g. "java.lang.Thread")
* @param methodName a method name
* @param descriptor a method descriptor (in JVM's format)
* @return this
*/
public Builder allowBlockingCallsInside(String className, String methodName, String descriptor) {
allowances
.computeIfAbsent(className, __ -> new HashMap<>())
.computeIfAbsent(methodName, __ -> new HashMap<>())
.put(descriptor, true);
return this;
}

public Builder disallowBlockingCallsInside(String className, String methodName) {
allowances.computeIfAbsent(className, __ -> new HashMap<>()).put(methodName, false);
return disallowBlockingCallsInside(className, methodName, "*");
}

/**
* Disallows blocking calls inside a method of a class with name identified by the provided className
* and that matches both provided methodName and descriptor.
*
* The descriptor should be in JVM's format:
* https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp276
*
* @param className class' name (e.g. "java.lang.Thread")
* @param methodName a method name
* @param descriptor a method descriptor (in JVM's format)
* @return this
*/
public Builder disallowBlockingCallsInside(String className, String methodName, String descriptor) {
allowances
.computeIfAbsent(className, __ -> new HashMap<>())
.computeIfAbsent(methodName, __ -> new HashMap<>())
.put(descriptor, false);
return this;
}

Expand Down