Skip to content

Commit

Permalink
Merge pull request #40 from byeongkeunahn/main
Browse files Browse the repository at this point in the history
Shorten the generated code with `./release-rs.sh --features short`
  • Loading branch information
kiwiyou authored Dec 9, 2023
2 parents 2a2663a + a3dadbc commit fff8aa3
Show file tree
Hide file tree
Showing 37 changed files with 873 additions and 145 deletions.
50 changes: 50 additions & 0 deletions .cargo/x86_64-unknown-linux-gnu-short.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"arch": "x86_64",
"cpu": "x86-64",
"crt-objects-fallback": "false",
"crt-static-respected": true,
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
"dynamic-linking": true,
"eh-frame-header": false,
"env": "gnu",
"has-rpath": true,
"has-thread-local": true,
"is-builtin": false,
"linker-flavor": "gnu-cc",
"llvm-target": "x86_64-unknown-linux-gnu",
"max-atomic-width": 64,
"os": "linux",
"plt-by-default": false,
"position-independent-executables": true,
"pre-link-args": {
"gnu-cc": [
"-m64"
],
"gnu-lld-cc": [
"-m64"
]
},
"relro-level": "off",
"stack-probes": {
"kind": "none"
},
"static-position-independent-executables": true,
"supported-sanitizers": [
"address",
"cfi",
"leak",
"memory",
"thread",
"safestack"
],
"supported-split-debuginfo": [
"packed",
"unpacked",
"off"
],
"supports-xray": true,
"target-family": [
"unix"
],
"target-pointer-width": "64"
}
27 changes: 26 additions & 1 deletion .github/workflows/build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
with:
toolchain: nightly
target: ${{ matrix.target }}
profile: minimal
profile: default
override: true
components: clippy
- name: Install node.js
Expand All @@ -39,6 +39,7 @@ jobs:
run: |
sudo apt update
sudo apt install gcc-multilib nasm
rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
- name: Clippy
run: cargo clippy
env:
Expand All @@ -53,15 +54,27 @@ jobs:
run: |
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release.sh C 64 ./src/solution.rs ./tests/boj_1000.in ./tests/boj_1000.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release.sh C 64 ./tests/boj_1001.rs ./tests/boj_1001.in ./tests/boj_1001.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release.sh C 64 ./tests/boj_2587.rs ./tests/boj_2587.in ./tests/boj_2587.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release.sh C 64 ./tests/boj_2751.rs ./tests/boj_2751.in.zip ./tests/boj_2751.out.zip
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release.sh C 64 ./tests/boj_3745.rs ./tests/boj_3745.in ./tests/boj_3745.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release.sh C 64 ./tests/boj_14939.rs ./tests/boj_14939.in ./tests/boj_14939.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release.sh C 64 ./tests/reloc.rs ./tests/reloc.in ./tests/reloc.out
- name: Check C (x86_64) - short
if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }}
run: |
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-short.sh C 64 ./src/solution.rs ./tests/boj_1000.in ./tests/boj_1000.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-short.sh C 64 ./tests/boj_1001.rs ./tests/boj_1001.in ./tests/boj_1001.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-short.sh C 64 ./tests/boj_2587.rs ./tests/boj_2587.in ./tests/boj_2587.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-short.sh C 64 ./tests/boj_2751.rs ./tests/boj_2751.in.zip ./tests/boj_2751.out.zip
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-short.sh C 64 ./tests/boj_3745.rs ./tests/boj_3745.in ./tests/boj_3745.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-short.sh C 64 ./tests/boj_14939.rs ./tests/boj_14939.in ./tests/boj_14939.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-short.sh C 64 ./tests/reloc.rs ./tests/reloc.in ./tests/reloc.out
- name: Check C (x86)
if: ${{ matrix.target == 'i686-unknown-linux-gnu' }}
run: |
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-32bit.sh C 32 ./src/solution.rs ./tests/boj_1000.in ./tests/boj_1000.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-32bit.sh C 32 ./tests/boj_1001.rs ./tests/boj_1001.in ./tests/boj_1001.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-32bit.sh C 32 ./tests/boj_2587.rs ./tests/boj_2587.in ./tests/boj_2587.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-32bit.sh C 32 ./tests/boj_2751.rs ./tests/boj_2751.in.zip ./tests/boj_2751.out.zip
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-32bit.sh C 32 ./tests/boj_3745.rs ./tests/boj_3745.in ./tests/boj_3745.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-32bit.sh C 32 ./tests/boj_14939.rs ./tests/boj_14939.in ./tests/boj_14939.out
Expand All @@ -71,15 +84,27 @@ jobs:
run: |
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-rs.sh Rust 64 ./src/solution.rs ./tests/boj_1000.in ./tests/boj_1000.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-rs.sh Rust 64 ./tests/boj_1001.rs ./tests/boj_1001.in ./tests/boj_1001.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-rs.sh Rust 64 ./tests/boj_2587.rs ./tests/boj_2587.in ./tests/boj_2587.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-rs.sh Rust 64 ./tests/boj_2751.rs ./tests/boj_2751.in.zip ./tests/boj_2751.out.zip
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-rs.sh Rust 64 ./tests/boj_3745.rs ./tests/boj_3745.in ./tests/boj_3745.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-rs.sh Rust 64 ./tests/boj_14939.rs ./tests/boj_14939.in ./tests/boj_14939.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-rs.sh Rust 64 ./tests/reloc.rs ./tests/reloc.in ./tests/reloc.out
- name: Check Rust (x86_64) - short
if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }}
run: |
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-rs-short.sh Rust 64 ./src/solution.rs ./tests/boj_1000.in ./tests/boj_1000.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-rs-short.sh Rust 64 ./tests/boj_1001.rs ./tests/boj_1001.in ./tests/boj_1001.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-rs-short.sh Rust 64 ./tests/boj_2587.rs ./tests/boj_2587.in ./tests/boj_2587.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-rs-short.sh Rust 64 ./tests/boj_2751.rs ./tests/boj_2751.in.zip ./tests/boj_2751.out.zip
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-rs-short.sh Rust 64 ./tests/boj_3745.rs ./tests/boj_3745.in ./tests/boj_3745.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-rs-short.sh Rust 64 ./tests/boj_14939.rs ./tests/boj_14939.in ./tests/boj_14939.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./.github/workflows/release-rs-short.sh Rust 64 ./tests/reloc.rs ./tests/reloc.in ./tests/reloc.out
- name: Check wasm32
if: ${{ matrix.target == 'wasm32-unknown-unknown' }}
run: |
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-wasm32.sh JavaScript 32 ./src/solution.rs ./tests/boj_1000.in ./tests/boj_1000.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-wasm32.sh JavaScript 32 ./tests/boj_1001.rs ./tests/boj_1001.in ./tests/boj_1001.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-wasm32.sh JavaScript 32 ./tests/boj_2587.rs ./tests/boj_2587.in ./tests/boj_2587.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-wasm32.sh JavaScript 32 ./tests/boj_2751.rs ./tests/boj_2751.in.zip ./tests/boj_2751.out.zip
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-wasm32.sh JavaScript 32 ./tests/boj_3745.rs ./tests/boj_3745.in ./tests/boj_3745.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-wasm32.sh JavaScript 32 ./tests/boj_14939.rs ./tests/boj_14939.in ./tests/boj_14939.out
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
run: |
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows.cmd C 64 .\src\solution.rs .\tests\boj_1000.in .\tests\boj_1000.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows.cmd C 64 .\tests\boj_1001.rs .\tests\boj_1001.in .\tests\boj_1001.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows.cmd C 64 .\tests\boj_2587.rs .\tests\boj_2587.in .\tests\boj_2587.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows.cmd C 64 .\tests\boj_2751.rs .\tests\boj_2751.in.zip .\tests\boj_2751.out.zip
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows.cmd C 64 .\tests\boj_3745.rs .\tests\boj_3745.in .\tests\boj_3745.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows.cmd C 64 .\tests\boj_14939.rs .\tests\boj_14939.in .\tests\boj_14939.out
Expand All @@ -64,6 +65,7 @@ jobs:
run: |
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows-rs.cmd Rust 64 .\src\solution.rs .\tests\boj_1000.in .\tests\boj_1000.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows-rs.cmd Rust 64 .\tests\boj_1001.rs .\tests\boj_1001.in .\tests\boj_1001.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows-rs.cmd Rust 64 .\tests\boj_2587.rs .\tests\boj_2587.in .\tests\boj_2587.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows-rs.cmd Rust 64 .\tests\boj_2751.rs .\tests\boj_2751.in.zip .\tests\boj_2751.out.zip
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows-rs.cmd Rust 64 .\tests\boj_3745.rs .\tests\boj_3745.in .\tests\boj_3745.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows-rs.cmd Rust 64 .\tests\boj_14939.rs .\tests\boj_14939.in .\tests\boj_14939.out
Expand All @@ -73,6 +75,7 @@ jobs:
run: |
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-wasm32.cmd JavaScript 32 .\src\solution.rs .\tests\boj_1000.in .\tests\boj_1000.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-wasm32.cmd JavaScript 32 .\tests\boj_1001.rs .\tests\boj_1001.in .\tests\boj_1001.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-wasm32.cmd JavaScript 32 .\tests\boj_2587.rs .\tests\boj_2587.in .\tests\boj_2587.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-wasm32.cmd JavaScript 32 .\tests\boj_2751.rs .\tests\boj_2751.in.zip .\tests\boj_2751.out.zip
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-wasm32.cmd JavaScript 32 .\tests\boj_3745.rs .\tests\boj_3745.in .\tests\boj_3745.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-wasm32.cmd JavaScript 32 .\tests\boj_14939.rs .\tests\boj_14939.in .\tests\boj_14939.out
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release-rs-short.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./release-rs.sh --features short
1 change: 1 addition & 0 deletions .github/workflows/release-short.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./release.sh --features short
16 changes: 16 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
"$rustc"
],
},
{
"label": "build-release-amd64-short-submit",
"type": "shell",
"command": "mkdir -p ./target/x86_64-unknown-linux-gnu-short/release; ./release.sh --features short > ./target/x86_64-unknown-linux-gnu-short/release/loader.c; code --reuse-window ./target/x86_64-unknown-linux-gnu-short/release/loader.c",
"problemMatcher": [
"$rustc"
],
},
{
"label": "build-release-amd64-rs-submit",
"type": "shell",
Expand All @@ -35,6 +43,14 @@
"$rustc"
],
},
{
"label": "build-release-amd64-rs-short-submit",
"type": "shell",
"command": "mkdir -p ./target/x86_64-unknown-linux-gnu-short/release; ./release-rs.sh --features short > ./target/x86_64-unknown-linux-gnu-short/release/loader.rs; code --reuse-window ./target/x86_64-unknown-linux-gnu-short/release/loader.rs",
"problemMatcher": [
"$rustc"
],
},
{
"label": "build-release-i686-submit",
"type": "shell",
Expand Down
13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@ bench = false
path = "src/bin/basm-submit.rs"

[dependencies]
compiler_builtins = { version = "0.1.101", features = ["mem"] }
libm = "0.2.7"
ryu = "1.0"

[target.x86_64-pc-windows-msvc.dependencies]
compiler_builtins = { version = "0.1.103", features = ["mem"] }
[target.x86_64-unknown-linux-gnu.dependencies]
compiler_builtins = { version = "0.1.103", features = ["mem"] }
[target.i686-unknown-linux-gnu.dependencies]
compiler_builtins = { version = "0.1.103", features = ["mem"] }
[target.wasm32-unknown-unknown.dependencies]
compiler_builtins = { version = "0.1.103", features = ["mem"] }

[features]
short = []

[profile.dev]
panic = "abort"

Expand Down
8 changes: 4 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
link_args_basm.push("/EMITPOGOPHASEINFO");
link_args_basm_submit.push("/ALIGN:128");
},
"x86_64-unknown-linux-gnu" | "i686-unknown-linux-gnu" => {
"x86_64-unknown-linux-gnu" | "x86_64-unknown-linux-gnu-short" | "i686-unknown-linux-gnu" => {
link_args_basm.push("-nostartfiles");
link_args_basm.push("-nostdlib");
link_args_basm.push("-static-pie");
Expand All @@ -27,12 +27,12 @@ fn main() {
link_args_basm.push("-fno-unwind-tables");
link_args_basm.push("-fno-stack-protector");
link_args_basm.push("-fno-plt");
if target == "x86_64-unknown-linux-gnu" {
link_args_basm.push("-Wl,--build-id=none,--gc-sections,--no-eh-frame-hdr,-z,norelro");
} else {
if target == "i686-unknown-linux-gnu" {
// Prevent linker from putting data into text, which is non-writable and hence not relocatable.
// This prevents the hack for getting the _DYNAMIC symbol in the entrypoint.
link_args_basm.push("-Wl,--build-id=none,--gc-sections,--no-eh-frame-hdr,-z,norelro,-z,notext");
} else {
link_args_basm.push("-Wl,--build-id=none,--gc-sections,--no-eh-frame-hdr,-z,norelro");
}
link_args_basm_submit.push("-Wl,-z,max-page-size=128");
},
Expand Down
2 changes: 1 addition & 1 deletion release-html.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
>&2 echo "Building project for target wasm32-unknown-unknown, language JavaScript, build mode Release"
cargo +nightly build --target wasm32-unknown-unknown --bin=basm-submit --release "$@"
python scripts/wasm-gen.py scripts/wasm-template.html
python3 scripts/wasm-gen.py scripts/wasm-template.html
2 changes: 1 addition & 1 deletion release-wasm32.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
>&2 echo "Building project for target wasm32-unknown-unknown, language JavaScript, build mode Release"
cargo +nightly build --target wasm32-unknown-unknown --bin=basm-submit --release "$@"
python scripts/wasm-gen.py scripts/wasm-template.js
python3 scripts/wasm-gen.py scripts/wasm-template.js
40 changes: 34 additions & 6 deletions scripts/base91.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
def encode(x):
def encode(x_in, use_rle=False):
sharp_insertion_points = []
if use_rle:
current_bits, current_bytes, i = 0, 0, 0
x = bytearray()
while i < len(x_in):
current_bits += 13
while current_bytes < current_bits // 8:
if i >= len(x_in):
break
x.append(x_in[i])
current_bytes += 1
i += 1
if len(x) > 0 and x[-1] == 0:
zeros_cnt = 1
while i - 1 + zeros_cnt < len(x_in) and zeros_cnt < 256 and x_in[i - 1 + zeros_cnt] == 0:
zeros_cnt += 1
if zeros_cnt >= 2:
x.pop()
x.append(zeros_cnt - 1)
sharp_insertion_points.append((current_bits // 13 * 2) + len(sharp_insertion_points))
i += zeros_cnt - 1
sharp_insertion_points = list(reversed(sharp_insertion_points))
else:
x = x_in

out = []
i = 0
cnt5, stack5 = 0, []
Expand All @@ -21,11 +46,14 @@ def encode(x):
if bits < 13:
stack5.append((pos, bits))
cnt5 -= 8
ret = bytearray(2 * len(out) + 1)
for i in range(len(out)):
ret[2 * i + 0] = 0x24 + (out[i] % 91)
ret[2 * i + 1] = 0x24 + (out[i] // 91)
ret[-1] = ord('!')
ret = bytearray()
for pack in out:
ret.append(0x24 + (pack % 91))
ret.append(0x24 + (pack // 91))
if len(sharp_insertion_points) > 0 and len(ret) == sharp_insertion_points[-1]:
ret.append(ord(b'#'))
sharp_insertion_points.pop()
ret.append(ord(b'!'))
return bytes(ret)

if __name__ == '__main__':
Expand Down
23 changes: 22 additions & 1 deletion scripts/static-pie-elf2bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,28 @@ def load_elf64(elf):
continue # since bytearray is zero-initialized

dst_off, src_off, cnt = sh_dict['sh_addr'], sh_dict['sh_offset'], sh_dict['sh_size']
memory_bin[dst_off:dst_off+cnt] = elf[src_off:src_off+cnt]
blob = elf[src_off:src_off+cnt]

if sh_dict['sh_type'] == SHT_DYNAMIC:
# Trim the DYNAMIC section, leaving only relocation-related entries
# 16 == sizeof(Elf64_Dyn)
dst = 0
for src in range(0, len(blob), 16):
# Included entries:
# DT_PLTRELSZ = 2, DT_RELA = 7, DT_RELASZ = 8, DT_RELAENT = 9,
# DT_REL = 17, DT_RELSZ = 18, DT_RELENT = 19, DT_PLTREL = 20,
# DT_TEXT_REL = 22, DT_JMPREL = 23.
#
# Note: DT_RELACOUNT = 0x6fff_fff9 and DT_RELCOUNT = 0x6fff_fffa
# are not included since they are redundant since
# DT_RELACOUNT = DT_RELASZ/DT_RELAENT and
# DT_RELCOUNT = DT_RELSZ/DT_RELENT.
if b2i(blob[src:src+8]) in [2, 7, 8, 9, 17, 18, 19, 20, 22, 23]:
blob[dst:dst+16] = blob[src:src+16]
dst += 16
blob[dst:] = bytearray(len(blob[dst:])) # fill remaining part with zeros

memory_bin[dst_off:dst_off+cnt] = blob

entrypoint_offset = b2i(elf[24:32])
return memory_bin, pos_begin, entrypoint_offset
Expand Down
Loading

0 comments on commit fff8aa3

Please sign in to comment.