Skip to content

Commit 44a3bec

Browse files
committed
Fixes + comments & documentation. Can now be used as a template
* adapted to use the new api layout of ARTist * updated with changed signatures from ARTist (updated module-sdk) * added comments and documentation * created 2 artist passes to showcase different use cases and existing boilerplate code, one injection pass (HInjectionArtist) and one basic pass (HArtist) * added small filter to only accept non-static methods (and skip the majority to not bloat logcat too much)
1 parent 9f7bb3c commit 44a3bec

14 files changed

+422
-113
lines changed

CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ set(CMAKE_CXX_STANDARD 11)
66
add_executable(template_module
77
src/module.cc
88
src/module.h
9-
src/instrumentation_pass.cc
10-
src/instrumentation_pass.h
9+
src/template_injection_pass.cc
10+
src/template_injection_pass.h
11+
src/template_basic_pass.cc
12+
src/template_basic_pass.h
1113
src/codelib.cc
12-
src/codelib.h)
14+
src/codelib.h src/simple_method_filter.cc src/simple_method_filter.h)
1315

1416
target_include_directories(template_module PUBLIC
1517
/opt/artist-sdk/include/external/valgrind

Manifest.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"manifest_version": 1,
3-
"package_name": "com.example.module",
4-
"name": "Artist Module",
5-
"description": "This a template module.",
6-
"author": "John Doe <[email protected]>",
3+
"package_name": "saarland.cispa.artist.module.template",
4+
"name": "Template Module",
5+
"description": "This a template module, all functionality is purely educational. If you want ",
6+
"author": "Parthipan Ramesh <[email protected]>, Oliver Schranz <[email protected]>",
77
"version": 1
88
}

codelib.apk

8.16 KB
Binary file not shown.

src/codelib.cc

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,31 @@
1616
* limitations under the License.
1717
*
1818
* @author "Parthipan Ramesh <[email protected]>"
19+
* @author "Oliver Schranz <[email protected]>"
1920
*
2021
*/
2122

2223
#include "codelib.h"
2324

24-
unordered_set<string> &ModuleCodeLib::getMethods() const {
25-
// TODO: implement methods like example below
26-
// static string instanceField = "Lsaarland/cispa/artist/codelib/CodeLib;traceLog()V";
27-
// static unordered_set<string> methods({instanceField});
28-
static unordered_set<string> methods({});
29-
return methods;
25+
const std::string TemplateCodeLib::INJECTION_ARTIST_TARGET("Lsaarland/cispa/artist/codelib/CodeLib;injectionArtistTarget(I)V");
26+
const std::string TemplateCodeLib::BASIC_ARTIST_TARGET("Lsaarland/cispa/artist/codelib/CodeLib;basicArtistTarget(ILjava/lang/Object;)V");
27+
28+
29+
unordered_set<string> &TemplateCodeLib::getMethods() const {
30+
// here we expose the signatures of codelib methods that are meant to be used from ARTist
31+
static unordered_set<string> methods({INJECTION_ARTIST_TARGET, BASIC_ARTIST_TARGET});
32+
return methods;
3033
}
3134

32-
string &ModuleCodeLib::getInstanceField() const {
33-
// TODO: implement instance fields like example below
34-
// static string instanceField = "Lsaarland/cispa/artist/codelib/CodeLib;INSTANCE";
35-
static string instanceField;
36-
return instanceField;
35+
string &TemplateCodeLib::getInstanceField() const {
36+
// here we expose the static field with our singleton instance. This will be used by ARTist to obtain an instance
37+
// of the codelib to call methods on it.
38+
static string instanceField = "Lsaarland/cispa/artist/codelib/CodeLib;INSTANCE";
39+
return instanceField;
3740
}
3841

39-
string &ModuleCodeLib::getCodeClass() const {
40-
// TODO: implement code class like example below
41-
// static string codeClass = "Lsaarland/cispa/artist/codelib/CodeLib;";
42-
static string codeClass;
43-
return codeClass;
42+
string &TemplateCodeLib::getCodeClass() const {
43+
// here we expose the codelib's java class to be able to load it in ARTist
44+
static string codeClass = "Lsaarland/cispa/artist/codelib/CodeLib;";
45+
return codeClass;
4446
}

src/codelib.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*
1818
* @author "Parthipan Ramesh <[email protected]>"
19+
* @author "Oliver Schranz <[email protected]>"
1920
*
2021
*/
2122

@@ -24,32 +25,36 @@
2425

2526
#include <string>
2627
#include <unordered_set>
27-
#include <artist/env/codelib.h>
28+
#include <artist/api/modules/codelib.h>
2829

2930
using std::string;
3031
using std::unordered_set;
3132

3233
using art::CodeLib;
3334

34-
class ModuleCodeLib : public CodeLib {
35+
class TemplateCodeLib : public CodeLib {
3536
public:
36-
ModuleCodeLib() = default;
37+
static const string INJECTION_ARTIST_TARGET;
38+
static const string BASIC_ARTIST_TARGET;
3739

38-
ModuleCodeLib(const ModuleCodeLib &other) = default;
3940

40-
ModuleCodeLib(ModuleCodeLib &&other) = default;
41+
TemplateCodeLib() = default;
4142

42-
~ModuleCodeLib() override = default;
43+
TemplateCodeLib(const TemplateCodeLib &other) = default;
4344

44-
ModuleCodeLib &operator=(const ModuleCodeLib &) = default;
45+
TemplateCodeLib(TemplateCodeLib &&other) = default;
4546

46-
ModuleCodeLib &operator=(ModuleCodeLib &&) = default;
47+
~TemplateCodeLib() override = default;
48+
49+
TemplateCodeLib &operator=(const TemplateCodeLib &) = default;
50+
51+
TemplateCodeLib &operator=(TemplateCodeLib &&) = default;
4752

4853
unordered_set<string> &getMethods() const override;
4954

5055
string &getInstanceField() const override;
5156

5257
string &getCodeClass() const override;
53-
}; // class ModuleCodeLib
58+
}; // class TemplateCodeLib
5459

5560
#endif // ART_MODULES_CODELIB_H_

src/instrumentation_pass.cc

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/module.cc

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,51 @@
1616
* limitations under the License.
1717
*
1818
* @author "Parthipan Ramesh <[email protected]>"
19+
* @author "Oliver Schranz <[email protected]>"
1920
*
2021
*/
2122

22-
#include <artist/filtering/method_name_filters.h>
23+
#include <artist/api/filtering/method_name_filters.h>
2324

2425
#include "module.h"
25-
#include "instrumentation_pass.h"
26+
//#include "template_basic_pass.h"
27+
#include "template_injection_pass.h"
2628
#include "codelib.h"
29+
#include "simple_method_filter.h"
2730

2831
using std::make_shared;
2932
using std::unique_ptr;
3033

31-
shared_ptr<HArtist> ArtistModule::createPass(const MethodInfo &method_info) const {
32-
return make_shared<HModule>(method_info);
34+
using art::ModuleId;
35+
36+
TemplateModule::TemplateModule(const shared_ptr<const art::FilesystemHelper> fs) : Module(fs) {}
37+
38+
HArtist * TemplateModule::createPass(const MethodInfo &method_info) const {
39+
// Due to the *clone bug* (https://github.com/Project-ARTist/ARTist/issues/10), we can only define one pass per
40+
// module right now, but this will change as soon as this bug is resolved.
41+
return new (method_info.GetGraph()->GetArena()) HTemplateInjectionArtist(method_info);
42+
// return new (method_info.GetGraph()->GetArena()) HTemplateBasicArtist(method_info);
3343
}
3444

35-
shared_ptr<const CodeLib> ArtistModule::createCodeLib() const {
36-
return make_shared<ModuleCodeLib>();
45+
shared_ptr<const CodeLib> TemplateModule::createCodeLib() const {
46+
return make_shared<TemplateCodeLib>();
3747
}
3848

39-
// Possible MethodFilter: skip android support lib ui methods since they bloat up the log
40-
unique_ptr<Filter> ArtistModule::getMethodFilter() const {
41-
const vector<const string> ui = {"android.support."};
42-
return unique_ptr<Filter>(new art::BlacklistFilter(ui));
49+
50+
// Here we can restrict for which methods our module should be executed.
51+
unique_ptr<Filter> TemplateModule::getMethodFilter() const {
52+
// creating blacklists/whitelists for method names is straightforward:
53+
// const vector<const string> ui = {"android.support."};
54+
// return unique_ptr<Filter>(new MethodNameBlacklist(ui));
55+
56+
// but here we use a custom filter that only excepts non-static methods (and only a fragment of the others)
57+
return unique_ptr<Filter>(new SimpleMethodFilter());
58+
}
59+
// the module factory
60+
extern "C" shared_ptr<Module> create(shared_ptr<const FilesystemHelper> fshelper) {
61+
return make_shared<TemplateModule>(fshelper);
4362
}
4463

45-
// the class factories
46-
extern "C" shared_ptr<art::Module> create() {
47-
return make_shared<ArtistModule>();
64+
extern "C" ModuleId get_id() {
65+
return "saarland.cispa.artist.module.template";
4866
}

src/module.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,31 @@
1616
* limitations under the License.
1717
*
1818
* @author "Parthipan Ramesh <[email protected]>"
19+
* @author "Oliver Schranz <[email protected]>"
1920
*
2021
*/
2122

2223
#ifndef ART_MODULES__MODULE_H_
2324
#define ART_MODULES__MODULE_H_
2425

25-
#include <artist/modules/module.h>
26+
#include <artist/api/modules/module.h>
2627

2728
using art::Module;
2829
using art::HArtist;
2930
using art::MethodInfo;
3031
using art::CodeLib;
3132
using art::Filter;
33+
using art::FilesystemHelper;
3234

33-
class ArtistModule : public Module {
34-
shared_ptr<HArtist> createPass(const MethodInfo &method_info) const OVERRIDE;
35+
class TemplateModule : public Module {
36+
HArtist * createPass(const MethodInfo &method_info) const OVERRIDE;
3537

36-
shared_ptr<const CodeLib> createCodeLib() const OVERRIDE;
38+
shared_ptr<const CodeLib> createCodeLib() const OVERRIDE;
3739

3840
public:
39-
unique_ptr<Filter> getMethodFilter() const OVERRIDE;
41+
explicit TemplateModule(const shared_ptr<const FilesystemHelper> fs);
42+
43+
unique_ptr<Filter> getMethodFilter() const OVERRIDE;
4044
};
4145

4246
#endif // ART_MODULES__MODULE_H_

src/simple_method_filter.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* The ARTist Project (https://artist.cispa.saarland)
3+
*
4+
* Copyright (C) 2018 CISPA (https://cispa.saarland), Saarland University
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @author "Oliver Schranz <[email protected]>"
19+
*
20+
*/
21+
22+
#include "simple_method_filter.h"
23+
24+
int SimpleMethodFilter::MOD = 1000000;
25+
26+
/**
27+
* Simple filter implementation that rejects all static methods and for all others only accepts after seeing `MOD`
28+
* methods. The reason is that in our (template) injection we write a lot to logcat and hence should avoid to inject
29+
* calls to this method too often.
30+
*
31+
* @param info information & data about the currently compiled method
32+
* @return
33+
*/
34+
bool SimpleMethodFilter::accept(const art::MethodInfo &info) {
35+
if (info.IsStatic()) {
36+
return false;
37+
}
38+
39+
mtx.lock();
40+
bool res = count == 0;
41+
count = (count + 1) % SimpleMethodFilter::MOD;
42+
mtx.unlock();
43+
return res;
44+
}
45+

src/simple_method_filter.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* The ARTist Project (https://artist.cispa.saarland)
3+
*
4+
* Copyright (C) 2018 CISPA (https://cispa.saarland), Saarland University
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @author "Oliver Schranz <[email protected]>"
19+
*
20+
*/
21+
22+
23+
#ifndef ART_MODULES__STATICMETHODFILTER_H
24+
#define ART_MODULES__STATICMETHODFILTER_H
25+
26+
#include <artist/api/filtering/filter.h>
27+
28+
using std::mutex;
29+
30+
using art::Filter;
31+
32+
class SimpleMethodFilter : public Filter {
33+
public:
34+
SimpleMethodFilter() : count(0) {};
35+
36+
bool accept(const art::MethodInfo &info) override;
37+
private:
38+
// after how many non-static methods we will accept one (restrict number of instrumented methods)
39+
static int MOD;
40+
41+
// mutex for atomic `check and write` operation on `count`
42+
mutex mtx;
43+
// how many methods did we check already
44+
int count;
45+
};
46+
47+
#endif //ART_MODULES__STATICMETHODFILTER_H

0 commit comments

Comments
 (0)