removing old JNI test, replaced by a configuration wizard in Cooja

This commit is contained in:
fros4943 2009-03-09 17:46:48 +00:00
parent 25ca251a89
commit eb9865ce79
13 changed files with 18 additions and 1315 deletions

View file

@ -0,0 +1,18 @@
The JNI tests have been replaced by the Cooja configuration wizard.
The JNI tests assisted in configuring Cooja for compiling and linking
Java Native Interface (JNI) enabled Contiki libraries. Such Contiki
libraries are used by Cooja's Contiki Motes: motes simulated at the
operating system abstraction level.
The new configuration wizard is started from inside Cooja, and exercises
the same functionality as the JNI tests. In contrast, the wizard
is directly connected to the current Cooja configuration, removing the
need to migrate the configuration between JNI tests and Cooja.
To start the wizard:
* Start COOJA:
tools/cooja> ant run
* Menu > Settings > Compiler configuration wizard
-- Fredrik Osterlind, fros@sics.se, March 2009

View file

@ -1,250 +0,0 @@
<?xml version="1.0"?>
<project name="COOJA Simulator - JNI Tests" default="about" basedir=".">
<property environment="env"/>
<target name="help">
<echo>
The majority of problems encountered during these tests are related to
the compilation and linking stage of the Contiki libraries.
Below follows a number of configurations that work for different users.
Linux users with the GNU toolchain may try:
PATH_C_COMPILER = gcc
COMPILER_ARGS =
LINK_COMMAND_1 = gcc -shared -Wl,-Map=$(MAPFILE) -o $(LIBFILE)
LINK_COMMAND_2 =
PARSE_WITH_COMMAND=false
Windows users with recent Cygwin may try:
PATH_C_COMPILER = gcc
COMPILER_ARGS = -mno-cygwin -Wall -I'$(JAVA_HOME)/include' -I'$(JAVA_HOME)/include/win32'
LINK_COMMAND_1 = gcc -mno-cygwin -shared -Wl,-Map=$(MAPFILE) -Wl,--add-stdcall-alias -o $(LIBFILE)
LINK_COMMAND_2 = -L/usr/lib/mingw -lmsvcrt
Windows users with older Cygwin may try:
PATH_C_COMPILER = gcc
COMPILER_ARGS = -mno-cygwin -I'$(JAVA_HOME)/include' -I'$(JAVA_HOME)/include/win32'
LINK_COMMAND_1 = ld -Map=$(MAPFILE) -shared --add-stdcall-alias /usr/lib/mingw/dllcrt2.o -o $(LIBFILE)
LINK_COMMAND_2 = -L/usr/lib/mingw -lmingw32 -lmingwex -lmsvcrt
Windows users with recent Cygwin and separate MinGW tools may try:
In recent (early 2007) MinGW-Cygwin versions, the JNI support has been limited.
This may cause COOJA not to be able to load libraries generated using gcc's -mno-cygwin flag.
One possible solution is to download "real" MinGW (http://www.mingw.org), and install it separately
from Cygwin (for example in c:\mingw). Try using the following settings:
PATH_C_COMPILER = gcc
COMPILER_ARGS = -Wall -D_JNI_IMPLEMENTATION_ -I'$(JAVA_HOME)/include' -I'$(JAVA_HOME)/include/win32'
LINK_COMMAND_1 = gcc -shared -Wl,-Map=$(MAPFILE) -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -o $(LIBFILE)
LINK_COMMAND_2 =
Mac users may try (see Mac note below):
PATH_C_COMPILER = gcc
PARSE_WITH_COMMAND = true
PARSE_COMMAND = nmandsize $(LIBFILE)
COMPILER_ARGS = -Wall -I/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Headers -dynamiclib -fno-common
LINK_COMMAND_1 = gcc -dynamiclib -fno-common -o $(LIBFILE)
LINK_COMMAND_2 = -framework JavaVM
Mac note:
The Mac OS X support is experimental, and currently depends on an external script to work.
The script 'nmandsize' is available in the subdirectory 'mac_users' and depends on both 'nm' and 'size'.
The purpose of the script is to gather information which COOJA uses to determine the memory sections addresses.
Observe that, if used, this script must be in the path.
</echo>
</target>
<target name="about">
<echo>
The COOJA Simulator - JNI Tests
-------------------------------
These tests can be used to help understand COOJA errors, and to configure COOJA for new users.
For COOJA to compile JNI libraries successfully, tests 2-5 must be completed.
In level3, only level3a or level3b is necessary to pass.
You may have to change the configuration in the file 'exttools.cfg'.
When all tests pass, these settings should be entered into the COOJA External tool settings dialog.
Before running the tests, the COOJA JAR file must be created:
> ant compile_cooja
To run the first test:
> ant level2
For more information including configuration examples:
> ant help
> ant level2
[compilation test]
Compiles level2.c to level2.library, using both c compiler and linker.
Java class loads the library and calls a simple native function.
> ant level3a
[address parsing using map file]
> ant level3b
[address parsing using command]
> ant level4
[fetching reference var]
Calculates offset between relative and absolute memory.
A simple native function increases two counters (from both data and bss sections).
> ant level5
[fetches and restores memory segments - the final test]
A simple native function increases two counters (from both data and bss sections).
The current memory (data+bss sections) is fetched and restored between function calls.
The counters should be restored with the memory!
</echo>
</target>
<target name="clean">
<delete>
<fileset dir="." includes="**/*.class" />
<fileset dir="." includes="**/*.library" />
<fileset dir="." includes="**/*.o" />
<fileset dir="." includes="**/*.map" />
<fileset dir="." includes="**/*.log" />
<fileset dir="." includes="build.temp" />
</delete>
</target>
<target name="init">
<tstamp/>
</target>
<target name="compile_library" depends="init">
<property name="SRCFILE" value="${LEVEL}.c"/>
<property name="OBJFILE" value="${LEVEL}.o"/>
<property name="LIBFILE" value="${LEVEL}.library"/>
<property name="MAPFILE" value="${LEVEL}.map"/>
<property name="ARFILE" value="${LEVEL}.a"/>
<property file="exttools.cfg"/>
<property name="COMPILE_COMMAND" value="${PATH_C_COMPILER} ${COMPILER_ARGS} -c ${SRCFILE} -o ${OBJFILE}"/>
<property name="LINKER_COMMAND" value="${LINK_COMMAND_1} ${OBJFILE} ${LINK_COMMAND_2}"/>
<propertyfile file="build.temp">
<entry key="COMPILE_EXEC" value="${COMPILE_COMMAND}"/>
<entry key="COMPILE_ARGS" value="${COMPILE_COMMAND}"/>
<entry key="LINKER_EXEC" value="${LINKER_COMMAND}"/>
<entry key="LINKER_ARGS" value="${LINKER_COMMAND}"/>
</propertyfile>
<replace file="build.temp" token="$(JAVA_HOME)" value="${env.JAVA_HOME}"/>
<replace file="build.temp" token="$(LIBFILE)" value="${LIBFILE}"/>
<replace file="build.temp" token="$(MAPFILE)" value="${MAPFILE}"/>
<replace file="build.temp" token="$(ARFILE)" value="${ARFILE}"/>
<replaceregexp
file="build.temp" match="COMPILE_EXEC=(\w*).*"
replace="COMPILE_EXEC=\1" byline="true"/>
<replaceregexp
file="build.temp" match="COMPILE_ARGS=\w*(.*)"
replace="COMPILE_ARGS=\1" byline="true"/>
<replaceregexp
file="build.temp" match="LINKER_EXEC=(\w*).*"
replace="LINKER_EXEC=\1" byline="true"/>
<replaceregexp
file="build.temp" match="LINKER_ARGS=\w*(.*)"
replace="LINKER_ARGS=\1" byline="true"/>
<property file="build.temp"/>
<echo message="EXECUTING: ${COMPILE_EXEC} ${COMPILE_ARGS}"/>
<exec dir="${LEVEL}" executable="${COMPILE_EXEC}" failonerror="true">
<arg line="${COMPILE_ARGS}"/>
</exec>
<echo message="EXECUTING: ${LINKER_EXEC} ${LINKER_ARGS}"/>
<exec dir="${LEVEL}" executable="${LINKER_EXEC}" failonerror="true">
<arg line="${LINKER_ARGS}"/>
</exec>
</target>
<target name="compile_cooja" depends="init">
<ant dir="../../" antfile="build.xml" target="jar"/>
</target>
<target name="level2" depends="init">
<property name="LEVEL" value="level2"/>
<antcall target="compile_library" inheritall="true"/>
<javac srcdir="${LEVEL}" destdir="${LEVEL}"/>
<java fork="yes" dir="${LEVEL}" classname="Level2"/>
</target>
<target name="level3">
<echo>
Run either
> ant level3a
or
> ant level3b
3a tests parsing library addresses using the map file generated at link time.
3b tests parsing library addresses using a command.
At least one of the tests must pass.
After passing, the 'exttools.cfg' must be updated.
The external tools setting:
PARSE_WITH_COMMAND=true
causes COOJA (and the following tests) to use the command to parse library addresses,
and should be used if test 3b passed.
PARSE_WITH_COMMAND=false
causes COOJA to parse the generated map file, and should be used if test 3a passed.
</echo>
</target>
<target name="level3a" depends="init">
<property name="LEVEL" value="level3a"/>
<antcall target="compile_library" inheritall="true"/>
<javac srcdir="${LEVEL}" destdir="${LEVEL}" classpath="../../dist/cooja.jar"/>
<java fork="yes" dir="${LEVEL}" classname="Level3a">
<classpath>
<pathelement location="../../dist/cooja.jar"/>
<pathelement location="${LEVEL}"/>
</classpath>
</java>
</target>
<target name="level3b" depends="init">
<property name="LEVEL" value="level3b"/>
<antcall target="compile_library" inheritall="true"/>
<javac srcdir="${LEVEL}" destdir="${LEVEL}" classpath="../../dist/cooja.jar"/>
<java fork="yes" dir="${LEVEL}" classname="Level3b">
<classpath>
<pathelement location="../../dist/cooja.jar"/>
<pathelement location="${LEVEL}"/>
</classpath>
</java>
</target>
<target name="level4" depends="init">
<property name="LEVEL" value="level4"/>
<antcall target="compile_library" inheritall="true"/>
<javac srcdir="${LEVEL}" destdir="${LEVEL}" classpath="../../dist/cooja.jar"/>
<java fork="yes" dir="${LEVEL}" classname="Level4">
<classpath>
<pathelement location="../../dist/cooja.jar"/>
<pathelement location="${LEVEL}"/>
</classpath>
</java>
</target>
<target name="level5" depends="init">
<property name="LEVEL" value="level5"/>
<antcall target="compile_library" inheritall="true"/>
<javac srcdir="${LEVEL}" destdir="${LEVEL}" classpath="../../dist/cooja.jar"/>
<java fork="yes" dir="${LEVEL}" classname="Level5">
<classpath>
<pathelement location="../../dist/cooja.jar"/>
<pathelement location="${LEVEL}"/>
</classpath>
</java>
</target>
</project>

View file

@ -1,10 +0,0 @@
# EXTERNAL TOOLS CONFIGURATION
# Change these until all tests pass!
# The working configuration must manually be entered in COOJA
PATH_C_COMPILER = gcc
COMPILER_ARGS = -Wall -D_JNI_IMPLEMENTATION_ -I'$(JAVA_HOME)/include' -I'$(JAVA_HOME)/include/win32'
LINK_COMMAND_1 = gcc -shared -Wl,-Map=$(MAPFILE) -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -o $(LIBFILE)
LINK_COMMAND_2 =
PARSE_WITH_COMMAND=true
PARSE_COMMAND=nm -C $(LIBFILE)

View file

@ -1,52 +0,0 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: Level2.java,v 1.2 2007/09/10 14:07:12 fros4943 Exp $
*/
import java.io.*;
public class Level2 {
static {
System.out.println("Loading library now");
System.load(new File("level2.library").getAbsolutePath());
}
private native void test();
public Level2() {
System.err.println("Calling native test function");
test();
}
public static void main(String[] args) {
new Level2();
}
}

View file

@ -1,40 +0,0 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: level2.c,v 1.2 2007/09/10 14:07:12 fros4943 Exp $
*/
#include <jni.h>
#include <stdio.h>
JNIEXPORT void JNICALL
Java_Level2_test(JNIEnv *env, jobject obj)
{
fprintf(stderr, "Level 2 OK!\n");
fflush(stderr);
}

View file

@ -1,140 +0,0 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: Level3a.java,v 1.1 2007/09/10 14:06:03 fros4943 Exp $
*/
import java.io.*;
import java.util.*;
import org.apache.log4j.xml.DOMConfigurator;
import se.sics.cooja.GUI;
import se.sics.cooja.contikimote.ContikiMoteType;
public class Level3a {
private final File externalToolsSettingsFile = new File("../exttools.cfg");
public Level3a() {
// Configure logger
DOMConfigurator.configure(GUI.class.getResource("/" + GUI.LOG_CONFIG_FILE));
// Load configuration
System.out.println("Loading COOJA configuration");
GUI.externalToolsUserSettingsFile = externalToolsSettingsFile;
GUI.loadExternalToolsDefaultSettings();
GUI.loadExternalToolsUserSettings();
System.out.println("Locating map file");
File mapFile = new File("level3a.map");
if (!mapFile.exists()) {
System.err.println("No map file could be loaded");
System.exit(1);
}
System.out.println("Loading map data");
Vector<String> mapData = ContikiMoteType.loadMapFile(mapFile);
if (mapData == null) {
System.err.println("No map data could be loaded");
System.exit(1);
}
System.out.println("Parsing map data");
Properties addresses = new Properties();
boolean parseOK = ContikiMoteType.parseMapFileData(mapData, addresses);
if (!parseOK) {
System.err.println("Map data parsing failed");
System.exit(1);
}
int relDataSectionAddr = ContikiMoteType.loadRelDataSectionAddr(mapData);
int dataSectionSize = ContikiMoteType.loadDataSectionSize(mapData);
int relBssSectionAddr = ContikiMoteType.loadRelBssSectionAddr(mapData);
int bssSectionSize = ContikiMoteType.loadBssSectionSize(mapData);
System.out.println("Found relative data section address: 0x" + Integer.toHexString(relDataSectionAddr));
System.out.println("Found data section size: 0x" + Integer.toHexString(dataSectionSize));
System.out.println("Found relative bss section address: 0x" + Integer.toHexString(relBssSectionAddr));
System.out.println("Found bss section size: 0x" + Integer.toHexString(bssSectionSize));
System.out.println("Checking validity of parsed addresses");
String varName;
varName = "var1";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
varName = "var2";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
varName = "arr1";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
varName = "arr2";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
varName = "uvar1";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
varName = "uvar2";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
if (relDataSectionAddr < 0) {
System.err.println("Data segment address < 0: 0x" + Integer.toHexString(relDataSectionAddr));
System.exit(1);
}
if (relBssSectionAddr < 0) {
System.err.println("BSS segment address < 0: 0x" + Integer.toHexString(relBssSectionAddr));
System.exit(1);
}
if (dataSectionSize <= 0) {
System.err.println("Data segment size <= 0: 0x" + Integer.toHexString(dataSectionSize));
System.exit(1);
}
if (bssSectionSize <= 0) {
System.err.println("BSS segment size <= 0: 0x" + Integer.toHexString(bssSectionSize));
System.exit(1);
}
System.out.println("Level 3a OK!");
}
public static void main(String[] args) {
new Level3a();
}
}

View file

@ -1,46 +0,0 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: level3a.c,v 1.1 2007/09/10 14:06:03 fros4943 Exp $
*/
#include <jni.h>
#include <stdio.h>
int var1=1;
int var2=2;
int var3=2;
int arr1[10];
int arr2[10];
int uvar1;
int uvar2;
JNIEXPORT void JNICALL
Java_Level3a_test(JNIEnv *env, jobject obj)
{
}

View file

@ -1,142 +0,0 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: Level3b.java,v 1.2 2007/09/18 11:35:10 fros4943 Exp $
*/
import java.io.*;
import java.util.*;
import org.apache.log4j.xml.DOMConfigurator;
import se.sics.cooja.GUI;
import se.sics.cooja.contikimote.ContikiMoteType;
public class Level3b {
private final File externalToolsSettingsFile = new File("../exttools.cfg");
public Level3b() {
// Configure logger
DOMConfigurator.configure(GUI.class.getResource("/" + GUI.LOG_CONFIG_FILE));
// Load configuration
System.out.println("Loading COOJA configuration");
GUI.externalToolsUserSettingsFile = externalToolsSettingsFile;
GUI.loadExternalToolsDefaultSettings();
GUI.loadExternalToolsUserSettings();
System.out.println("Using parse command settings: \n" +
"\tPARSE_COMMAND = "+ GUI.getExternalToolsSetting("PARSE_COMMAND"));
System.out.println("Locating library file");
File libFile = new File("level3b.library");
if (!libFile.exists()) {
System.err.println("Library file " + libFile.getAbsolutePath() + " could not be found!");
System.exit(1);
}
System.out.println("Loading command data");
Vector<String> commandData = ContikiMoteType.loadCommandData(libFile);
if (commandData == null) {
System.err.println("No command data could be loaded");
System.exit(1);
}
System.out.println("Parsing command data");
Properties addresses = new Properties();
boolean parseOK = ContikiMoteType.parseCommandData(commandData, addresses);
if (!parseOK) {
System.err.println("Command data parsing failed");
System.exit(1);
}
int relDataSectionAddr = ContikiMoteType.loadCommandRelDataSectionAddr(commandData);
int dataSectionSize = ContikiMoteType.loadCommandDataSectionSize(commandData);
int relBssSectionAddr = ContikiMoteType.loadCommandRelBssSectionAddr(commandData);
int bssSectionSize = ContikiMoteType.loadCommandBssSectionSize(commandData);
System.out.println("Found relative data section address: 0x" + Integer.toHexString(relDataSectionAddr));
System.out.println("Found data section size: 0x" + Integer.toHexString(dataSectionSize));
System.out.println("Found relative bss section address: 0x" + Integer.toHexString(relBssSectionAddr));
System.out.println("Found bss section size: 0x" + Integer.toHexString(bssSectionSize));
System.out.println("Checking validity of parsed addresses");
String varName;
varName = "var1";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
varName = "var2";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
varName = "arr1";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
varName = "arr2";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
varName = "uvar1";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
varName = "uvar2";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
if (relDataSectionAddr < 0) {
System.err.println("Data segment address < 0: 0x" + Integer.toHexString(relDataSectionAddr));
System.exit(1);
}
if (relBssSectionAddr < 0) {
System.err.println("BSS segment address < 0: 0x" + Integer.toHexString(relBssSectionAddr));
System.exit(1);
}
if (dataSectionSize <= 0) {
System.err.println("Data segment size <= 0: 0x" + Integer.toHexString(dataSectionSize));
System.exit(1);
}
if (bssSectionSize <= 0) {
System.err.println("BSS segment size <= 0: 0x" + Integer.toHexString(bssSectionSize));
System.exit(1);
}
System.out.println("Level 3b OK!");
}
public static void main(String[] args) {
new Level3b();
}
}

View file

@ -1,46 +0,0 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: level3b.c,v 1.1 2007/09/10 14:06:23 fros4943 Exp $
*/
#include <jni.h>
#include <stdio.h>
int var1=1;
int var2=2;
int var3=2;
int arr1[10];
int arr2[10];
int uvar1;
int uvar2;
JNIEXPORT void JNICALL
Java_Level3b_test(JNIEnv *env, jobject obj)
{
}

View file

@ -1,174 +0,0 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: Level4.java,v 1.4 2008/11/20 16:22:28 fros4943 Exp $
*/
import java.io.*;
import java.util.Properties;
import java.util.Vector;
import org.apache.log4j.xml.DOMConfigurator;
import se.sics.cooja.GUI;
import se.sics.cooja.contikimote.ContikiMoteType;
public class Level4 {
private final File externalToolsSettingsFile = new File("../exttools.cfg");
static {
System.load(new File("level4.library").getAbsolutePath());
}
private native void doCount();
public native void setReferenceAddress(int addr);
public Level4() {
// Configure logger
DOMConfigurator.configure(GUI.class.getResource("/" + GUI.LOG_CONFIG_FILE));
// Load configuration
System.out.println("Loading COOJA configuration");
GUI.externalToolsUserSettingsFile = externalToolsSettingsFile;
GUI.loadExternalToolsDefaultSettings();
GUI.loadExternalToolsUserSettings();
// Should we parse addresses using map file or command?
boolean useCommand = Boolean.parseBoolean(GUI.getExternalToolsSetting("PARSE_WITH_COMMAND", "false"));
Properties addresses = new Properties();
int relDataSectionAddr = -1;
int dataSectionSize = -1;
int relBssSectionAddr = -1;
int bssSectionSize = -1;
if (useCommand) {
// Parse command output
System.out.println("Parsing using command");
File libFile = new File("level4.library");
if (!libFile.exists()) {
System.err.println("Library file " + libFile.getAbsolutePath() + " could not be found!");
System.exit(1);
}
Vector<String> commandData = ContikiMoteType.loadCommandData(libFile);
if (commandData == null) {
System.err.println("No command data could be loaded");
System.exit(1);
}
boolean parseOK = ContikiMoteType.parseCommandData(commandData, addresses);
if (!parseOK) {
System.err.println("Command data parsing failed");
System.exit(1);
}
relDataSectionAddr = ContikiMoteType.loadCommandRelDataSectionAddr(commandData);
dataSectionSize = ContikiMoteType.loadCommandDataSectionSize(commandData);
relBssSectionAddr = ContikiMoteType.loadCommandRelBssSectionAddr(commandData);
bssSectionSize = ContikiMoteType.loadCommandBssSectionSize(commandData);
} else {
// Parse map file
System.out.println("Parsing using map file");
File mapFile = new File("level4.map");
if (!mapFile.exists()) {
System.err.println("No map file could be loaded");
System.exit(1);
}
Vector<String> mapData = ContikiMoteType.loadMapFile(mapFile);
if (mapData == null) {
System.err.println("No map data could be loaded");
System.exit(1);
}
boolean parseOK = ContikiMoteType.parseMapFileData(mapData, addresses);
if (!parseOK) {
System.err.println("Map data parsing failed");
System.exit(1);
}
relDataSectionAddr = ContikiMoteType.loadRelDataSectionAddr(mapData);
dataSectionSize = ContikiMoteType.loadDataSectionSize(mapData);
relBssSectionAddr = ContikiMoteType.loadRelBssSectionAddr(mapData);
bssSectionSize = ContikiMoteType.loadBssSectionSize(mapData);
}
String varName;
varName = "initialized_counter";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
varName = "uninitialized_counter";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
varName = "ref_var";
if (!addresses.containsKey(varName)) {
System.err.println("Could not find address of: " + varName);
System.exit(1);
}
if (relDataSectionAddr < 0) {
System.err.println("Data segment address < 0: 0x" + Integer.toHexString(relDataSectionAddr));
System.exit(1);
}
if (relBssSectionAddr < 0) {
System.err.println("BSS segment address < 0: 0x" + Integer.toHexString(relBssSectionAddr));
System.exit(1);
}
if (dataSectionSize <= 0) {
System.err.println("Data segment size <= 0: 0x" + Integer.toHexString(dataSectionSize));
System.exit(1);
}
if (bssSectionSize <= 0) {
System.err.println("BSS segment size <= 0: 0x" + Integer.toHexString(bssSectionSize));
System.exit(1);
}
int relRefAddress = (Integer) addresses.get("ref_var");
System.out.println("Relative reference address: 0x" + Integer.toHexString(relRefAddress));
setReferenceAddress(relRefAddress);
doCount();
doCount();
doCount();
doCount();
doCount();
System.err.println("Level 4 OK!");
}
public static void main(String[] args) {
new Level4();
}
}

View file

@ -1,56 +0,0 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: level4.c,v 1.3 2008/11/20 16:22:28 fros4943 Exp $
*/
#include <jni.h>
#include <stdio.h>
long ref_var; /* Placed somewhere in the BSS section */
int initialized_counter=1; /* Variable in data section */
int uninitialized_counter; /* Variable in BSS section */
JNIEXPORT void JNICALL
Java_Level4_setReferenceAddress(JNIEnv *env, jobject obj, jint addr)
{
/*printf("relative reference address is %p\n", addr);*/
/*printf("absolute reference address is %p\n", &ref_var);*/
ref_var = (((long)&ref_var) - ((long)addr));
printf("Offset is 0x%p\n", ref_var);
fflush(stdout);
}
JNIEXPORT void JNICALL
Java_Level4_doCount(JNIEnv *env, jobject obj)
{
printf(">> DATA_counter=\t%i\tBSS_counter=\t%i\n", initialized_counter++, uninitialized_counter++);
fflush(stdout);
}

View file

@ -1,278 +0,0 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: Level5.java,v 1.5 2008/11/20 16:22:28 fros4943 Exp $
*/
import java.io.*;
import java.util.Properties;
import java.util.Vector;
import org.apache.log4j.BasicConfigurator;
import se.sics.cooja.GUI;
import se.sics.cooja.SectionMoteMemory;
import se.sics.cooja.contikimote.ContikiMoteType;
public class Level5 {
private final File externalToolsSettingsFile = new File("../exttools.cfg");
static {
System.load(new File("level5.library").getAbsolutePath());
}
private native void doCount();
public native void setReferenceAddress(int addr);
public native void getMemory(int addr, int length, byte[] mem);
public native void setMemory(int addr, int length, byte[] mem);
private int javaDataCounter = 1;
private int javaBssCounter = 0;
public Level5() {
// Configure logger
BasicConfigurator.configure();
// Load configuration
System.out.println("Loading COOJA configuration");
GUI.externalToolsUserSettingsFile = externalToolsSettingsFile;
GUI.loadExternalToolsDefaultSettings();
GUI.loadExternalToolsUserSettings();
// Should we parse addresses using map file or command?
boolean useCommand = Boolean.parseBoolean(GUI.getExternalToolsSetting("PARSE_WITH_COMMAND", "false"));
Properties addresses = new Properties();
int relDataSectionAddr = -1;
int dataSectionSize = -1;
int relBssSectionAddr = -1;
int bssSectionSize = -1;
if (useCommand) {
// Parse command output
System.out.println("Parsing using command");
File libFile = new File("level5.library");
if (!libFile.exists()) {
System.err.println("Library file " + libFile.getAbsolutePath() + " could not be found!");
System.exit(1);
}
Vector<String> commandData = ContikiMoteType.loadCommandData(libFile);
if (commandData == null) {
System.err.println("No command data could be loaded");
System.exit(1);
}
boolean parseOK = ContikiMoteType.parseCommandData(commandData, addresses);
if (!parseOK) {
System.err.println("Command data parsing failed");
System.exit(1);
}
relDataSectionAddr = ContikiMoteType.loadCommandRelDataSectionAddr(commandData);
dataSectionSize = ContikiMoteType.loadCommandDataSectionSize(commandData);
relBssSectionAddr = ContikiMoteType.loadCommandRelBssSectionAddr(commandData);
bssSectionSize = ContikiMoteType.loadCommandBssSectionSize(commandData);
} else {
// Parse map file
System.out.println("Parsing using map file");
File mapFile = new File("level5.map");
if (!mapFile.exists()) {
System.err.println("No map file could be loaded");
System.exit(1);
}
Vector<String> mapData = ContikiMoteType.loadMapFile(mapFile);
if (mapData == null) {
System.err.println("No map data could be loaded");
System.exit(1);
}
boolean parseOK = ContikiMoteType.parseMapFileData(mapData, addresses);
if (!parseOK) {
System.err.println("Map data parsing failed");
System.exit(1);
}
relDataSectionAddr = ContikiMoteType.loadRelDataSectionAddr(mapData);
dataSectionSize = ContikiMoteType.loadDataSectionSize(mapData);
relBssSectionAddr = ContikiMoteType.loadRelBssSectionAddr(mapData);
bssSectionSize = ContikiMoteType.loadBssSectionSize(mapData);
}
int relRefAddress = (Integer) addresses.get("ref_var");
setReferenceAddress(relRefAddress);
System.out.println("Creating section memory");
byte[] initialDataSection = new byte[dataSectionSize];
byte[] initialBssSection = new byte[bssSectionSize];
getMemory(relDataSectionAddr, dataSectionSize, initialDataSection);
getMemory(relBssSectionAddr, bssSectionSize, initialBssSection);
SectionMoteMemory memory = new SectionMoteMemory(addresses);
memory.setMemorySegment(relDataSectionAddr, initialDataSection);
memory.setMemorySegment(relBssSectionAddr, initialBssSection);
int dataCounter, bssCounter;
System.out.print("Checking initial values: ");
dataCounter = memory.getIntValueOf("initialized_counter");
bssCounter = memory.getIntValueOf("uninitialized_counter");
if (dataCounter != javaDataCounter) {
System.out.println("DATA mismatch (" + dataCounter + " != " + javaDataCounter + ")");
System.out.println("FAILED!");
System.exit(1);
} else if (bssCounter != javaBssCounter) {
System.out.println("BSS mismatch (" + bssCounter + " != " + javaBssCounter + ")");
System.out.println("FAILED!");
System.exit(1);
} else {
System.out.println("OK!");
}
System.out.println("Increasing counters 5 times");
doCount(); javaDataCounter++; javaBssCounter++;
doCount(); javaDataCounter++; javaBssCounter++;
doCount(); javaDataCounter++; javaBssCounter++;
doCount(); javaDataCounter++; javaBssCounter++;
doCount(); javaDataCounter++; javaBssCounter++;
System.out.print("Checking increased values: ");
getMemory(relDataSectionAddr, dataSectionSize, initialDataSection);
getMemory(relBssSectionAddr, bssSectionSize, initialBssSection);
memory.setMemorySegment(relDataSectionAddr, initialDataSection);
memory.setMemorySegment(relBssSectionAddr, initialBssSection);
dataCounter = memory.getIntValueOf("initialized_counter");
bssCounter = memory.getIntValueOf("uninitialized_counter");
if (dataCounter != javaDataCounter) {
System.out.println("DATA mismatch (" + dataCounter + " != " + javaDataCounter + ")");
System.out.println("FAILED!");
System.exit(1);
} else if (bssCounter != javaBssCounter) {
System.out.println("BSS mismatch (" + bssCounter + " != " + javaBssCounter + ")");
System.out.println("FAILED!");
System.exit(1);
} else {
System.out.println("OK!");
}
System.out.println("Storing both memory segments now");
byte[] savedDataSection = new byte[dataSectionSize];
byte[] savedBssSection = new byte[bssSectionSize];
int savedDataCounter = dataCounter;
int savedBssCounter = bssCounter;
getMemory(relDataSectionAddr, dataSectionSize, savedDataSection);
getMemory(relBssSectionAddr, bssSectionSize, savedBssSection);
System.out.println("Increasing counters 3 times");
doCount(); javaDataCounter++; javaBssCounter++;
doCount(); javaDataCounter++; javaBssCounter++;
doCount(); javaDataCounter++; javaBssCounter++;
System.out.print("Checking increased values: ");
getMemory(relDataSectionAddr, dataSectionSize, initialDataSection);
getMemory(relBssSectionAddr, bssSectionSize, initialBssSection);
memory.setMemorySegment(relDataSectionAddr, initialDataSection);
memory.setMemorySegment(relBssSectionAddr, initialBssSection);
dataCounter = memory.getIntValueOf("initialized_counter");
bssCounter = memory.getIntValueOf("uninitialized_counter");
if (dataCounter != javaDataCounter) {
System.out.println("DATA mismatch (" + dataCounter + " != " + javaDataCounter + ")");
System.out.println("FAILED!");
System.exit(1);
} else if (bssCounter != javaBssCounter) {
System.out.println("BSS mismatch (" + bssCounter + " != " + javaBssCounter + ")");
System.out.println("FAILED!");
System.exit(1);
} else {
System.out.println("OK!");
}
System.out.println("Restoring data segment");
setMemory(relDataSectionAddr, dataSectionSize, savedDataSection);
javaDataCounter = savedDataCounter;
System.out.println("Increasing counters 3 times");
doCount(); javaDataCounter++; javaBssCounter++;
doCount(); javaDataCounter++; javaBssCounter++;
doCount(); javaDataCounter++; javaBssCounter++;
System.out.print("Checking reset data counter: ");
getMemory(relDataSectionAddr, dataSectionSize, initialDataSection);
getMemory(relBssSectionAddr, bssSectionSize, initialBssSection);
memory.setMemorySegment(relDataSectionAddr, initialDataSection);
memory.setMemorySegment(relBssSectionAddr, initialBssSection);
dataCounter = memory.getIntValueOf("initialized_counter");
bssCounter = memory.getIntValueOf("uninitialized_counter");
if (dataCounter != javaDataCounter) {
System.out.println("DATA mismatch (" + dataCounter + " != " + javaDataCounter + ")");
System.out.println("FAILED!");
System.exit(1);
} else if (bssCounter != javaBssCounter) {
System.out.println("BSS mismatch (" + bssCounter + " != " + javaBssCounter + ")");
System.out.println("FAILED!");
System.exit(1);
} else {
System.out.println("OK!");
}
System.out.println("Restoring bss segment");
setMemory(relBssSectionAddr, bssSectionSize, savedBssSection);
javaBssCounter = savedBssCounter;
System.out.print("Checking reset bss counter: ");
getMemory(relDataSectionAddr, dataSectionSize, initialDataSection);
getMemory(relBssSectionAddr, bssSectionSize, initialBssSection);
memory.setMemorySegment(relDataSectionAddr, initialDataSection);
memory.setMemorySegment(relBssSectionAddr, initialBssSection);
dataCounter = memory.getIntValueOf("initialized_counter");
bssCounter = memory.getIntValueOf("uninitialized_counter");
if (dataCounter != javaDataCounter) {
System.out.println("DATA mismatch (" + dataCounter + " != " + javaDataCounter + ")");
System.out.println("FAILED!");
System.exit(1);
} else if (bssCounter != javaBssCounter) {
System.out.println("BSS mismatch (" + bssCounter + " != " + javaBssCounter + ")");
System.out.println("FAILED!");
System.exit(1);
} else {
System.out.println("OK!");
}
System.out.println("\n");
System.out.println("Reading and writing memory segments via JNI successfully");
System.out.println("Level 5 OK!");
}
public static void main(String[] args) {
new Level5();
}
}

View file

@ -1,81 +0,0 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: level5.c,v 1.4 2008/11/20 16:22:28 fros4943 Exp $
*/
#include <jni.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
long ref_var; /* Placed somewhere in the BSS section */
int initialized_counter=1; /* Variable in data section */
int uninitialized_counter; /* Variable in BSS section */
JNIEXPORT void JNICALL
Java_Level5_setReferenceAddress(JNIEnv *env, jobject obj, jint addr)
{
/*printf("relative reference address is %p\n", addr);*/
/*printf("absolute reference address is %p\n", &ref_var);*/
ref_var = (((long)&ref_var) - ((long)addr));
printf("Offset is 0x%p\n", ref_var);
fflush(stdout);
}
JNIEXPORT void JNICALL
Java_Level5_doCount(JNIEnv *env, jobject obj)
{
printf(">> DATA_counter=\t%i\tBSS_counter=\t%i\n", ++initialized_counter, ++uninitialized_counter);
fflush(stdout);
}
JNIEXPORT void JNICALL
Java_Level5_getMemory(JNIEnv *env, jobject obj, jint rel_addr, jint length, jbyteArray mem_arr)
{
(*env)->SetByteArrayRegion(
env,
mem_arr,
0,
(size_t) length,
(jbyte *) (((long)rel_addr) + ref_var)
);
}
JNIEXPORT void JNICALL
Java_Level5_setMemory(JNIEnv *env, jobject obj, jint rel_addr, jint length, jbyteArray mem_arr)
{
jbyte *mem = (*env)->GetByteArrayElements(env, mem_arr, 0);
memcpy(
(char*) (((long)rel_addr) + ref_var),
mem,
length);
(*env)->ReleaseByteArrayElements(env, mem_arr, mem, 0);
}