Skip to content

Commit

Permalink
Proposed support for reading .btm scripts from a jar URL
Browse files Browse the repository at this point in the history
https://issues.redhat.com/projects/BYTEMAN/issues/BYTEMAN-437

Signed-off-by: Scott M Stark <starksm64@gmail.com>
  • Loading branch information
starksm64 authored and adinn committed Jan 10, 2024
1 parent 41be149 commit ebba8a4
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 6 deletions.
27 changes: 27 additions & 0 deletions agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
</manifestEntries>
</archive>
</configuration>
<!-- Create a tests artifact -->
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -2415,6 +2423,25 @@
<argLine>-Dorg.jboss.byteman.compile.to.bytecode -javaagent:${project.build.directory}/byteman-agent-${project.version}.jar=listener:true</argLine>
</configuration>
</execution>
<!-- Test accessing the script from a jar url -->
<execution>
<id>jar.TestScriptFromJar</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
<test>TestScriptFromJar</test>
<includes>
<include>org/jboss/byteman/tests/jar/TestScriptFromJar.class</include>
</includes>
<!-- access script from jar:file://...target/byteman-x.y.z-tests.jar!/scripts/jar/TestScriptFromJar.btm -->
<argLine>-Dorg.jboss.byteman.compile.to.bytecode -javaagent:${project.build.directory}/byteman-agent-${project.version}.jar=script:jar:file://${project.build.directory}/byteman-agent-${project.version}-tests.jar!/scripts/jar/TestScriptFromJar.btm</argLine>
</configuration>
</execution>
</executions>
</plugin>
<!--
Expand Down
21 changes: 15 additions & 6 deletions agent/src/main/java/org/jboss/byteman/agent/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.JarURLConnection;
import java.net.Socket;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -207,19 +209,26 @@ public static void premain(String args, Instrumentation inst)
// look up rules in any script files

for (String scriptPath : scriptPaths) {
FileInputStream fis = null;
InputStream is = null;
try {
fis = new FileInputStream(scriptPath);
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
if(scriptPath.startsWith("jar:")) {
// A jar:<url>!/{entry}
URL url = new URL(scriptPath);
JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
is = jarConnection.getJarFile().getInputStream(jarConnection.getJarEntry());
} else {
is = new FileInputStream(scriptPath);
}
byte[] bytes = new byte[is.available()];
is.read(bytes);
String ruleScript = new String(bytes);
scripts.add(ruleScript);
} catch (IOException ioe) {
System.err.println("org.jboss.byteman.agent.Main: unable to read rule script file : " + scriptPath);
throw ioe;
} finally {
if (fis != null)
fis.close();
if (is != null)
is.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2023, Red Hat and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*
* @authors Andrew Dinn
*/
package org.jboss.byteman.tests.jar;

import org.jboss.byteman.tests.Test;

/**
* Validate that a script loaded from a jar file is correctly applied
* can be run using:
* mvn package failsafe:integration-test@jar.TestScriptFromJar
*/
public class TestScriptFromJar extends Test {
public TestScriptFromJar()
{
super(TestScriptFromJar.class.getCanonicalName());
}

public void test()
{
log("calling TestScriptFromJar.triggerMethod1");
triggerMethod1();
log("called TestScriptFromJar.triggerMethod1");
}

public void triggerMethod1()
{
log("inside TestScriptFromJar.triggerMethod1");
}

public String getExpected()
{
logExpected("calling TestScriptFromJar.triggerMethod1");
logExpected("caller match first caller");
logExpected("inside TestScriptFromJar.triggerMethod1");
logExpected("called TestScriptFromJar.triggerMethod1");

return super.getExpected();
}
}
33 changes: 33 additions & 0 deletions agent/src/test/resources/scripts/jar/TestScriptFromJar.btm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
##############################################################################
# JBoss, Home of Professional Open Source
# Copyright 2010, Red Hat and individual contributors
# by the @authors tag. See the copyright.txt in the distribution for a
# full listing of individual contributors.
#
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this software; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
#
# @authors Andrew Dinn
#

RULE test caller match first caller
CLASS TestScriptFromJar
METHOD triggerMethod1()
HELPER org.jboss.byteman.tests.helpers.Default
AT ENTRY
BIND test : TestScriptFromJar = $0
IF callerEquals("test")
DO test.log("caller match first caller")
ENDRULE

0 comments on commit ebba8a4

Please sign in to comment.