Skip to content

Commit 3253084

Browse files
committed
scripts: Add script for calculating gadgets in generated binaries.
1 parent e285bff commit 3253084

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

crates/intrinsics/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ panic = "abort"
1111

1212
[profile.release]
1313
panic = "abort"
14+
lto = true
1415

1516
[dependencies]
1617
libc = "0.2.153"

scripts/gadgets.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# Counts the number of gadgets present in the output binaries from both Cobalt and `cobc`.
5+
# Usage: ./gadgets.sh [COBOL FILE]
6+
7+
8+
###############
9+
## CONSTANTS ##
10+
###############
11+
12+
RED='\033[0;31m'
13+
ORANGE='\033[0;33m'
14+
CYAN='\033[0;36m'
15+
GREEN='\033[1;32m'
16+
NC='\033[0m'
17+
18+
###############
19+
## FUNCTIONS ##
20+
###############
21+
22+
# Prints the usage pattern of this script.
23+
print_usage() {
24+
echo -e "${RED}Usage: ./gadgets.sh [COBOL-FILE]${NC}"
25+
}
26+
27+
################
28+
## ENTRYPOINT ##
29+
################
30+
31+
# Fetch the script directory.
32+
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
33+
34+
# Check that the number of arguments is valid.
35+
if [ $# -ne 1 ]; then
36+
echo -e "${RED}Invalid number of arguments.${NC}"
37+
print_usage
38+
exit -1
39+
fi
40+
41+
# Build Cobalt in release mode.
42+
cd "$SCRIPT_DIR/.."
43+
./build.sh release
44+
45+
# Create output binaries for each compiler.
46+
cd "$SCRIPT_DIR/.."
47+
./build/cobalt build -O speed_and_size -d ./gadgets_out -o cobalt_hws $1
48+
./build/cobalt build -O speed_and_size -d ./gadgets_out -o cobalt_nohws --disable-security-features $1
49+
cobc -x -free -O3 -o ./gadgets_out/cobc_std $1
50+
51+
# Observe gadget number for each binary.
52+
# For HWS, we also remove any `blr`/`br` gadgets because of BTI.
53+
cd gadgets_out
54+
COBALT_HWS_COUNT=$(\
55+
ROPgadget --binary cobalt_hws \
56+
| grep ^0x \
57+
| sed -E '/autiasp|paciasp/d' \
58+
| sed -E '/blr|br/d' \
59+
| wc -l \
60+
)
61+
COBALT_NOHWS_COUNT=$(\
62+
ROPgadget --binary cobalt_nohws \
63+
| grep ^0x \
64+
| sed -E '/autiasp|paciasp/d' \
65+
| wc -l \
66+
)
67+
COBC_COUNT=$(\
68+
ROPgadget --binary cobc_std \
69+
| grep ^0x \
70+
| sed -E '/autiasp|paciasp/d' \
71+
| wc -l \
72+
)
73+
74+
# Output results.
75+
echo "cobalt(pac+bti): $COBALT_HWS_COUNT gadgets."
76+
echo "cobalt(none): $COBALT_NOHWS_COUNT gadgets."
77+
echo "cobc(none): $COBC_COUNT gadgets."
78+
79+
# Remove artifacts.
80+
cd "$SCRIPT_DIR/.."
81+
rm -rf gadgets_out

0 commit comments

Comments
 (0)