Skip to content

Commit 4d26e2f

Browse files
authored
Merge pull request diffblue#337 from diffblue/models-merge
SEC-242: Update the models library in the security scanner repo.
2 parents d95e8f4 + 4bc98e4 commit 4d26e2f

File tree

2,968 files changed

+79371
-6037
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,968 files changed

+79371
-6037
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ execute_process(COMMAND ${boost-include_SOURCE_DIR}/b2 dll-path=${boost-include_
7070
WORKING_DIRECTORY ${boost-include_SOURCE_DIR}
7171
)
7272

73+
execute_process(COMMAND mvn package
74+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/benchmarks/LIBRARIES/models/model
75+
)
76+
7377
set(boost_include_include_dir ${boost-include_SOURCE_DIR})
7478
set(boost_lib_dir ${boost-include_SOURCE_DIR}/stage/lib)
7579
link_directories(${boost_lib_dir})
@@ -82,7 +86,6 @@ set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
8286
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
8387

8488
add_subdirectory(cbmc)
85-
add_subdirectory(benchmarks/LIBRARIES/models)
8689

8790
add_subdirectory(src)
8891

benchmarks/LIBRARIES/models/.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
target
2-
jdk-original
3-
plug/build
1+
/model/target
2+
/model/jdk-original
43

54
*.txt
65
*.d

benchmarks/LIBRARIES/models/.travis.yml

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ before_install:
2626
CLOUDSDK_CORE_DISABLE_PROMPTS=1; curl https://sdk.cloud.google.com | bash > /tmp/gcloud.log || cat /tmp/gcloud.log; fi
2727
- source /home/travis/google-cloud-sdk/path.bash.inc
2828
# Add gcloud to $PATH
29-
- gcloud --quiet components update
29+
# Temporarily disable gcloud update to avoid crash. See TG-2513 for details (and as a reminder to re-enable this).
30+
# gcloud --quiet components update
3031
- gcloud auth activate-service-account --key-file .gcloud-travis-models-library.json
3132
# Resolve dependencies for gsutil
32-
- pip install google_compute_engine --upgrade
33+
- sudo pip install google_compute_engine --upgrade
3334

3435
install:
3536
- cd model
3637
- mvn package
3738
- cd modelTests
38-
- gsutil cp gs://travis-artifacts/test-gen/${TEST_GEN_BUILD:-"latest-master-ubuntu"}.tar.gz cbmc-testgen-latest.tar.gz
39+
- gsutil cp gs://travis-artifacts/test-gen/${TEST_GEN_BUILD:-"latest-develop-ubuntu"}.tar.gz cbmc-testgen-latest.tar.gz
3940
- tar -zxvf cbmc-testgen-latest.tar.gz -C under_test
40-
- mvn install:install-file -Dfile=under_test/diffblue.jar -DpomFile=under_test/pom.xml
41+
- mvn install:install-file -Dfile=under_test/deeptestutils-1.0.0.jar -DpomFile=under_test/pom.xml
4142
- mv ../target/models.jar under_test
4243
# Setup gauge
4344
- sudo apt-key adv --keyserver hkp://pool.sks-keyservers.net --recv-keys 023EDB0B
@@ -46,20 +47,38 @@ install:
4647
- sudo apt-get install gauge
4748
- gauge install java
4849
- gauge install html-report
50+
- gauge config plugin_kill_timeout 20000
51+
- export MAVEN_OPTS="-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
52+
- export DIFFBLUE_MODEL_TESTS_PROCESS_TIMEOUT="500"
4953

5054
script:
51-
- mvn test -Dtags='!known-bug' -Dflags='-s' -DspecsDir='specs'
52-
- bash print-reports.sh reports/html-report/specs
55+
- mvn test -Dtags='!known-bug,!future,!long' -Dflags='--verbose' -DspecsDir='specs'
56+
57+
before_cache:
58+
# To be able to update via pip
59+
- sudo apt-get remove python-openssl
60+
# Upgrade pyopenssl (signurl prereq) via pip
61+
- sudo pip install pyopenssl --upgrade
62+
63+
- |
64+
# Save the html report
65+
( set -euo pipefail
66+
if [[ -d reports/html-report ]]
67+
then
68+
REPORT_FILE=html-report-$(git describe --tags --always).tar.gz
69+
tar -zcvf ${REPORT_FILE} reports/html-report
70+
gsutil -h "Content-Disposition:attachment; filename=models-${TRAVIS_BUILD_NUMBER}-report.tar.gz" \
71+
cp ${REPORT_FILE} gs://travis-artifacts/models-library/models-${TRAVIS_BUILD_NUMBER}-report.tar.gz
72+
echo Link to html report:
73+
echo "https://console.cloud.google.com/storage/browser/travis-artifacts/models-library?project=diffblue-cr&prefix=models-${TRAVIS_BUILD_NUMBER}"
74+
fi
75+
)
5376
5477
after_success:
5578
- |
5679
set -euo pipefail
5780
if [[ "${TRAVIS_PULL_REQUEST_BRANCH:-$TRAVIS_BRANCH}" =~ ^master|develop$ ]]
5881
then
59-
# To be able to update via pip
60-
sudo apt-get remove python-openssl
61-
# Upgrade pyopenssl (signurl prereq) via pip
62-
pip install pyopenssl --upgrade
6382
BRANCH=$(echo ${TRAVIS_PULL_REQUEST_BRANCH:-$TRAVIS_BRANCH} | tr '/' '_')
6483
FILE=models-$(git describe --tags --always).jar
6584
mv under_test/models.jar ${FILE}
@@ -78,5 +97,5 @@ after_success:
7897
SIGNED_URL=$(gsutil signurl -d 365d -m GET ${TRAVIS_BUILD_DIR}/.gcloud-travis-models-library.json \
7998
gs://diffblue-binaries/models-library/${BRANCH}/${FILE} | awk '$5 ~ /https/ {print $5}')
8099
fi
81-
set +u
100+
set +eu
82101
- echo ${SIGNED_URL}

benchmarks/LIBRARIES/models/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# These owners will be the default owners for everything in the repo.
2+
* @allredj @antlechner @romainbrenguier

benchmarks/LIBRARIES/models/README.md

Lines changed: 117 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,113 @@ interfaces of objects, avoiding reflection.
4141

4242
### Writing models
4343

44+
- Don't remove public static fields. Doing so could lead to invariant
45+
violations during test generation. Changing the value that is assigned to the
46+
field is fine.
4447
- Don't remove exceptions, unless you can do so while maintaining the
4548
documented behaviour of the method.
4649
- Use the methods in the CProver library to supply nondet variables, or to
47-
assume that certain invariants always hold. To use this library, `import
50+
assume that certain conditions always hold. To use this library, `import
4851
org.cprover.CProver;` at the top of your model class, and use the
49-
`CProver.nondet*` or `CProver.assume` methods.
52+
`CProver.nondet*` or `CProver.assume` methods. The library is located in
53+
`model/src/main/java/org/cprover/CProver.java`.
54+
For example, for a method that takes an argument that we do not currently
55+
support being null, you could add the line
56+
```
57+
CProver.assume(arg != null);
58+
```
59+
in that method, and for a method with return type `int` that should give
60+
nondeterministic results, you could write
61+
```
62+
return CProver.nondetInt();
63+
```
64+
Methods that are not currently modelled should include the line
65+
```
66+
CProver.notModelled();
67+
```
68+
in the method body. Deeptest will discard any tests that depend on any such
69+
method.
5070
- More information about CProver's assume functionality can be found in [the
5171
manual](http://www.cprover.org/cprover-manual/modeling-assertions.shtml).
5272
- CBMC is smart about strings. Most of the `java.lang.String` methods are
5373
intercepted and handled natively by CBMC. Therefore, it should be fine to
54-
keep `String`-based functionality in your models.
74+
keep `String`-based functionality in your models. However, when `String`
75+
operations are unlikely to be necessary it can be preferable to avoid them
76+
as they can slow down test generation in some cases. For example, we usually
77+
avoid detail messages in `Throwable`s when they would include string
78+
concatenation, e.g.
79+
```
80+
throw new IllegalArgumentException("Illegal Capacity: "+(initialCapacity));
81+
```
82+
would be commented out and replaced with a simple
83+
```
84+
throw new IllegalArgumentException();
85+
```
5586
- Unbounded loops are bad, so try to avoid those.
56-
- Run `mvn package` to check that your model builds.
57-
- Leave existing comments in the code, to keep diffs minimal.
87+
- Run `mvn package` from the `model` directory to check that your model builds.
88+
- Leave existing comments in the code to keep diffs minimal, and comment out
89+
original code from the JDK rather than delete it, to make it easier to review
90+
what the original implementation does and what was changed/simplified.
5891
- For non-trivial changes, please add a comment describing how the new modelled
5992
implementation differs from the original. So that this comment can be
6093
differentiated from existing comments, it should begin with
6194
```
6295
// DIFFBLUE MODEL LIBRARY
6396
```
6497

98+
### Generating the documentation
99+
100+
The javadoc for the model library can be generated from the `model` directory
101+
with the command:
102+
```
103+
mvn javadoc:javadoc
104+
```
105+
The generated site can then be found in
106+
`model/target/site/apidocs/index.html`.
107+
The javadoc interprets the following tags placed in field, method, and class
108+
headers:
109+
- @diffblue.fullSupport
110+
- @diffblue.limitedSupport
111+
- @diffblue.noSupport
112+
- @diffblue.untested
113+
- @diffblue.todo
114+
- @diffblue.mock
115+
116+
For instance, if the following tag is present in the header of a method:
117+
```
118+
/**
119+
* Returns a {@code Byte} instance representing the specified
120+
* {@code byte} value.
121+
*
122+
* @param b a byte value.
123+
* @return a {@code Byte} instance representing {@code b}.
124+
*
125+
* @diffblue.limitedSupport
126+
* Restricted to non-negative values.
127+
* Might be slow if argument is non-deterministic.
128+
*/
129+
public static Byte valueOf(byte b) {
130+
...
131+
```
132+
The following output can be observed in the documentation of the Byte class:
133+
<blockquote><b>DIFFBLUE: Limited support</b><br/>
134+
Restricted to non-negative values. Might be slow if argument is
135+
non-deterministic.
136+
</blockquote>
137+
138+
65139
### Running Tests
66140

67141
To test models with test-gen, you'll need an up-to-date copy of the
68142
test-generator binary. If you're developing models-library from the test-gen
69-
submodule, you can `cd model/modelTests` and run `setup.sh` to symlink
143+
submodule, you can `cd model/modelTests` and run `setup.sh "build"` to symlink
70144
`test-generator` and `models.jar` into the `under_test` directory.
71-
`setup.sh` will link `test-generator` from the outer test-gen repo, and
145+
`setup.sh "build"` will link `test-generator` from the outer test-gen repo, and
72146
`models.jar` from the `target` directory, so it's important that these files
73147
are in the expected locations, and have been built against the current branch
74-
of the models repo.
148+
of the models repo. The argument to `setup.sh` is the directory passed to cmake
149+
when building `test-generator`, so if you ran `cmake -H. -B$DIR`, you should
150+
run `setup.sh $DIR`.
75151

76152
Once you've set up the tests, you can run them using Gauge through Maven. Run
77153
the following command from the modelTests directory. It will get all the
@@ -111,6 +187,13 @@ report. To open it, run the following command from the `modelTests` directory.
111187
see reports/html-report/index.html
112188
```
113189

190+
Travis also provides a report after running all the models-library tests, which
191+
is saved as a Travis artifact and is hence available for 30 days. It can be
192+
accessed by following the link at the bottom of the Travis output and by
193+
unfolding the `# Save the html report` line. The link there will lead you to
194+
the file on Google Cloud.
195+
196+
114197
### Adding New Tests
115198

116199
You will probably want to supply test specifications alongside any new models.
@@ -164,7 +247,28 @@ executable) of the `main` method in the provided class. The second argument
164247
denotes the expected return code, and the final argument is a snippet that
165248
should be matched in the program's stdout.
166249

167-
#### Test Generation
250+
#### Grouped Test Generation (recommended)
251+
252+
The process for generating tests and verfying them (compile, run, pass) in a
253+
specified maven directory is as follows:
254+
255+
Set the maven directory and clean all test files:
256+
```
257+
* Setup Maven project "path/to/directory"
258+
```
259+
Run test-generator to populate the Maven project with generated test:
260+
```
261+
* Generate tests for "java.function.signature1" in "target/classes/test1.class"
262+
* Generate tests for "java.function.signature2" in "target/classes/test2.class"
263+
* Generate tests for "java.function.signature3" in "target/classes/test3.class"
264+
...
265+
```
266+
Run mvn test on the Maven project:
267+
```
268+
* Verify tests in Maven project
269+
```
270+
271+
#### Standalone Test Generation (not recommended)
168272

169273
```
170274
* Verify test case for function "java.function.signature" in file "../under_test/models.jar"
@@ -183,6 +287,9 @@ The last-generated test will be left in the current Maven directory, which can
183287
be useful for debugging in the cases where the Gauge output doesn't supply
184288
enough information.
185289

290+
This procedure is not recommended as the overhead for running the maven tests
291+
is several seconds. It is therefore better to group tests as suggested above.
292+
186293
##### Supplying Function Signatures
187294

188295
Most of the time, test-gen will be able to find the function you're testing
@@ -207,30 +314,6 @@ The important points:
207314
- End with `;`
208315
- Use `/` instead of `.` to separate class paths
209316

210-
### Adding Initialisation Code
211-
212-
This repo also contains some C++ code in the `plug` folder which is intended to
213-
be compiled into test-generator. This plugin tells test-gen how to initialise
214-
modelled classes, in the cases where test-generator can't work it out
215-
automatically using input-synthesis.
216-
217-
At the moment, the plugin works like this:
218-
219-
A function with the following signature is created, which returns a string of
220-
Java code initialising a variable with the given `name` to the state contained
221-
in `value`:
222-
```
223-
std::string classname_init(const struct_exprt &value, const std::string &name);
224-
```
225-
226-
Then, this function is registered into the `writer_table` map in
227-
`plug/java_init.cpp`. That's it!
228-
229-
Test-gen can query this map using the `get_model_writer` function to see
230-
whether there exists a custom initialiser for a given class. If so,
231-
test-generator will use this custom initialiser, and if not it will fall back
232-
to input synthesis or possibly reflection.
233-
234317
### Misc
235318

236319
`create-small-rt-jar.sh` can be used to create a jar file which contains the
@@ -264,3 +347,4 @@ variables set by the test-gen superbuild.
264347

265348
[build_img]: https://travis-ci.com/diffblue/models-library.svg?token=i8KzPhcTpXyyoppmAEw1
266349
[travis]: https://travis-ci.com/diffblue/models-library
350+

benchmarks/LIBRARIES/models/model/modelTests/.gitignore

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
logs
33
reports
44
gauge_bin
5-
target
5+
/target
66

7-
generated_test/src/test/java/MyTest.java
7+
**/src/test/java/TestFor*.java
88

99
under_test/models.jar
1010
under_test/test-generator
11+
12+
**/target/surefire-reports
13+
**/target/*.jar
14+
**/target/test-classes
15+
**/target/maven-archiver

0 commit comments

Comments
 (0)