From 3eafd8c20327fb4a488d6105bceb2bea9ddb8706 Mon Sep 17 00:00:00 2001 From: fas-devbot Date: Tue, 24 Nov 2026 21:21:22 +0000 Subject: [PATCH] Mayhem integration Co-Authored-By: Claude Opus 5.5 --- .github/workflows/mayhem.yml | 112 ++++++++++++ .github/workflows/sync.yml | 103 +++++++++++ mayhem/.gitignore | 2 + mayhem/Dockerfile | 35 ++++ mayhem/Dockerfile.dockerignore | 2 + mayhem/Mayhemfile_asr | 12 ++ mayhem/Mayhemfile_c | 12 ++ mayhem/Mayhemfile_cpp_python | 12 ++ mayhem/Mayhemfile_parse | 12 ++ mayhem/Mayhemfile_wasm_x64 | 12 ++ mayhem/Mayhemfile_wat | 12 ++ mayhem/asr/testsuite/array_01_decl.py | 43 +++++ mayhem/asr/testsuite/array_02_decl.py | 29 +++ mayhem/asr/testsuite/assert1.py | 5 + mayhem/asr/testsuite/assign2.py | 6 + mayhem/asr/testsuite/c_interop1.py | 32 ++++ mayhem/asr/testsuite/callback_01.py | 26 +++ mayhem/asr/testsuite/complex1.py | 38 ++++ mayhem/asr/testsuite/dictionary1.py | 41 +++++ mayhem/asr/testsuite/elemental_01.py | 165 ++++++++++++++++++ mayhem/asr/testsuite/expr1.py | 15 ++ mayhem/asr/testsuite/expr10.py | 31 ++++ mayhem/asr/testsuite/expr12.py | 13 ++ mayhem/asr/testsuite/expr13.py | 23 +++ mayhem/asr/testsuite/expr2.py | 11 ++ mayhem/asr/testsuite/expr4.py | 6 + mayhem/asr/testsuite/expr6.py | 7 + mayhem/asr/testsuite/expr7.py | 16 ++ mayhem/asr/testsuite/expr8.py | 23 +++ mayhem/asr/testsuite/expr_01.py | 28 +++ mayhem/asr/testsuite/expr_05.py | 89 ++++++++++ mayhem/asr/testsuite/expr_07.py | 29 +++ mayhem/asr/testsuite/expr_09.py | 48 +++++ mayhem/asr/testsuite/expr_10.py | 16 ++ mayhem/asr/testsuite/expr_14.py | 41 +++++ mayhem/asr/testsuite/func_inline_01.py | 16 ++ mayhem/asr/testsuite/global_scope1.py | 2 + mayhem/asr/testsuite/global_syms_01.py | 12 ++ mayhem/asr/testsuite/intent_01.py | 6 + mayhem/asr/testsuite/list1.py | 33 ++++ mayhem/build.sh | 90 ++++++++++ mayhem/c-backend/testsuite/c_interop1.py | 32 ++++ mayhem/c-backend/testsuite/expr7.py | 16 ++ mayhem/c-backend/testsuite/expr_01.py | 28 +++ mayhem/c-backend/testsuite/expr_11.py | 11 ++ mayhem/c-backend/testsuite/func_static_01.py | 15 ++ mayhem/c-backend/testsuite/import_order_01.py | 7 + mayhem/c-backend/testsuite/loop4.py | 11 ++ mayhem/c-backend/testsuite/test_import_02.py | 13 ++ mayhem/c-backend/testsuite/test_issue_518.py | 22 +++ .../c-backend/testsuite/variable_decl_03.py | 11 ++ mayhem/config.json | 4 + mayhem/cpp-python/testsuite/assert1.py | 5 + mayhem/cpp-python/testsuite/expr12.py | 13 ++ mayhem/cpp-python/testsuite/expr15.py | 25 +++ mayhem/cpp-python/testsuite/expr2.py | 11 ++ mayhem/cpp-python/testsuite/expr6.py | 7 + mayhem/cpp-python/testsuite/expr7.py | 16 ++ mayhem/cpp-python/testsuite/expr8.py | 23 +++ mayhem/cpp-python/testsuite/expr_11.py | 11 ++ mayhem/cpp-python/testsuite/loop3.py | 5 + mayhem/cpp-python/testsuite/loop4.py | 11 ++ .../cpp-python/testsuite/test_builtin_pow.py | 89 ++++++++++ .../cpp-python/testsuite/test_list_repeat2.py | 24 +++ mayhem/harnesses/fuzz_asr.cpp | 14 ++ mayhem/harnesses/fuzz_c.cpp | 18 ++ mayhem/harnesses/fuzz_cpp_python.cpp | 29 +++ mayhem/harnesses/fuzz_parse.cpp | 9 + mayhem/harnesses/fuzz_wasm_x64.cpp | 44 +++++ mayhem/harnesses/fuzz_wat.cpp | 16 ++ mayhem/harnesses/lpython_fuzz_common.h | 120 +++++++++++++ mayhem/lsan_off.cc | 1 + mayhem/parse/testsuite/assert1.py | 5 + mayhem/parse/testsuite/complex1.py | 38 ++++ mayhem/parse/testsuite/constants1.py | 118 +++++++++++++ mayhem/parse/testsuite/dictionary1.py | 41 +++++ mayhem/parse/testsuite/ellipsis1.py | 37 ++++ mayhem/parse/testsuite/expr1.py | 15 ++ mayhem/parse/testsuite/expr10.py | 31 ++++ mayhem/parse/testsuite/expr12.py | 13 ++ mayhem/parse/testsuite/expr13.py | 23 +++ mayhem/parse/testsuite/expr2.py | 11 ++ mayhem/parse/testsuite/expr4.py | 6 + mayhem/parse/testsuite/expr6.py | 7 + mayhem/parse/testsuite/expr7.py | 16 ++ mayhem/parse/testsuite/expr8.py | 23 +++ mayhem/parse/testsuite/expr_01.py | 9 + mayhem/parse/testsuite/global1.py | 26 +++ mayhem/parse/testsuite/global_scope1.py | 2 + mayhem/parse/testsuite/list1.py | 33 ++++ mayhem/parse/testsuite/loop3.py | 5 + mayhem/parse/testsuite/set1.py | 14 ++ mayhem/parse/testsuite/tuple1.py | 35 ++++ mayhem/reference_tests.py | 39 +++++ mayhem/test.sh | 61 +++++++ mayhem/wasm-x64/testsuite/bool1.py | Bin 0 -> 129 bytes mayhem/wasm-x64/testsuite/expr14.py | Bin 0 -> 203 bytes mayhem/wasm-x64/testsuite/expr2.py | Bin 0 -> 177 bytes mayhem/wasm-x64/testsuite/print_str.py | Bin 0 -> 188 bytes mayhem/wat/testsuite/bool1.py | 10 ++ mayhem/wat/testsuite/expr14.py | 12 ++ mayhem/wat/testsuite/expr2.py | 11 ++ mayhem/wat/testsuite/print_str.py | 13 ++ 103 files changed, 2581 insertions(+) create mode 100644 .github/workflows/mayhem.yml create mode 100644 .github/workflows/sync.yml create mode 100644 mayhem/.gitignore create mode 100644 mayhem/Dockerfile create mode 100644 mayhem/Dockerfile.dockerignore create mode 100644 mayhem/Mayhemfile_asr create mode 100644 mayhem/Mayhemfile_c create mode 100644 mayhem/Mayhemfile_cpp_python create mode 100644 mayhem/Mayhemfile_parse create mode 100644 mayhem/Mayhemfile_wasm_x64 create mode 100644 mayhem/Mayhemfile_wat create mode 100755 mayhem/asr/testsuite/array_01_decl.py create mode 100755 mayhem/asr/testsuite/array_02_decl.py create mode 100755 mayhem/asr/testsuite/assert1.py create mode 100755 mayhem/asr/testsuite/assign2.py create mode 100755 mayhem/asr/testsuite/c_interop1.py create mode 100755 mayhem/asr/testsuite/callback_01.py create mode 100755 mayhem/asr/testsuite/complex1.py create mode 100755 mayhem/asr/testsuite/dictionary1.py create mode 100755 mayhem/asr/testsuite/elemental_01.py create mode 100755 mayhem/asr/testsuite/expr1.py create mode 100755 mayhem/asr/testsuite/expr10.py create mode 100755 mayhem/asr/testsuite/expr12.py create mode 100755 mayhem/asr/testsuite/expr13.py create mode 100755 mayhem/asr/testsuite/expr2.py create mode 100755 mayhem/asr/testsuite/expr4.py create mode 100755 mayhem/asr/testsuite/expr6.py create mode 100755 mayhem/asr/testsuite/expr7.py create mode 100755 mayhem/asr/testsuite/expr8.py create mode 100755 mayhem/asr/testsuite/expr_01.py create mode 100755 mayhem/asr/testsuite/expr_05.py create mode 100755 mayhem/asr/testsuite/expr_07.py create mode 100755 mayhem/asr/testsuite/expr_09.py create mode 100755 mayhem/asr/testsuite/expr_10.py create mode 100755 mayhem/asr/testsuite/expr_14.py create mode 100755 mayhem/asr/testsuite/func_inline_01.py create mode 100755 mayhem/asr/testsuite/global_scope1.py create mode 100755 mayhem/asr/testsuite/global_syms_01.py create mode 100755 mayhem/asr/testsuite/intent_01.py create mode 100755 mayhem/asr/testsuite/list1.py create mode 100755 mayhem/build.sh create mode 100755 mayhem/c-backend/testsuite/c_interop1.py create mode 100755 mayhem/c-backend/testsuite/expr7.py create mode 100755 mayhem/c-backend/testsuite/expr_01.py create mode 100755 mayhem/c-backend/testsuite/expr_11.py create mode 100755 mayhem/c-backend/testsuite/func_static_01.py create mode 100755 mayhem/c-backend/testsuite/import_order_01.py create mode 100755 mayhem/c-backend/testsuite/loop4.py create mode 100755 mayhem/c-backend/testsuite/test_import_02.py create mode 100755 mayhem/c-backend/testsuite/test_issue_518.py create mode 100755 mayhem/c-backend/testsuite/variable_decl_03.py create mode 100644 mayhem/config.json create mode 100755 mayhem/cpp-python/testsuite/assert1.py create mode 100755 mayhem/cpp-python/testsuite/expr12.py create mode 100755 mayhem/cpp-python/testsuite/expr15.py create mode 100755 mayhem/cpp-python/testsuite/expr2.py create mode 100755 mayhem/cpp-python/testsuite/expr6.py create mode 100755 mayhem/cpp-python/testsuite/expr7.py create mode 100755 mayhem/cpp-python/testsuite/expr8.py create mode 100755 mayhem/cpp-python/testsuite/expr_11.py create mode 100755 mayhem/cpp-python/testsuite/loop3.py create mode 100755 mayhem/cpp-python/testsuite/loop4.py create mode 100755 mayhem/cpp-python/testsuite/test_builtin_pow.py create mode 100755 mayhem/cpp-python/testsuite/test_list_repeat2.py create mode 100644 mayhem/harnesses/fuzz_asr.cpp create mode 100644 mayhem/harnesses/fuzz_c.cpp create mode 100644 mayhem/harnesses/fuzz_cpp_python.cpp create mode 100644 mayhem/harnesses/fuzz_parse.cpp create mode 100644 mayhem/harnesses/fuzz_wasm_x64.cpp create mode 100644 mayhem/harnesses/fuzz_wat.cpp create mode 100644 mayhem/harnesses/lpython_fuzz_common.h create mode 100644 mayhem/lsan_off.cc create mode 100755 mayhem/parse/testsuite/assert1.py create mode 100755 mayhem/parse/testsuite/complex1.py create mode 100755 mayhem/parse/testsuite/constants1.py create mode 100755 mayhem/parse/testsuite/dictionary1.py create mode 100755 mayhem/parse/testsuite/ellipsis1.py create mode 100755 mayhem/parse/testsuite/expr1.py create mode 100755 mayhem/parse/testsuite/expr10.py create mode 100755 mayhem/parse/testsuite/expr12.py create mode 100755 mayhem/parse/testsuite/expr13.py create mode 100755 mayhem/parse/testsuite/expr2.py create mode 100755 mayhem/parse/testsuite/expr4.py create mode 100755 mayhem/parse/testsuite/expr6.py create mode 100755 mayhem/parse/testsuite/expr7.py create mode 100755 mayhem/parse/testsuite/expr8.py create mode 100755 mayhem/parse/testsuite/expr_01.py create mode 100755 mayhem/parse/testsuite/global1.py create mode 100755 mayhem/parse/testsuite/global_scope1.py create mode 100755 mayhem/parse/testsuite/list1.py create mode 100755 mayhem/parse/testsuite/loop3.py create mode 100755 mayhem/parse/testsuite/set1.py create mode 100755 mayhem/parse/testsuite/tuple1.py create mode 100755 mayhem/reference_tests.py create mode 100755 mayhem/test.sh create mode 100755 mayhem/wasm-x64/testsuite/bool1.py create mode 100755 mayhem/wasm-x64/testsuite/expr14.py create mode 100755 mayhem/wasm-x64/testsuite/expr2.py create mode 100755 mayhem/wasm-x64/testsuite/print_str.py create mode 100755 mayhem/wat/testsuite/bool1.py create mode 100755 mayhem/wat/testsuite/expr14.py create mode 100755 mayhem/wat/testsuite/expr2.py create mode 100755 mayhem/wat/testsuite/print_str.py diff --git a/.github/workflows/mayhem.yml b/.github/workflows/mayhem.yml new file mode 100644 index 0000000000..572937fa7b --- /dev/null +++ b/.github/workflows/mayhem.yml @@ -0,0 +1,112 @@ +# The Mayhem worker: build the commit image from mayhem/Dockerfile, push it to ghcr, then run +# Mayhem over each Mayhemfile. Runs on manual dispatch and is called by the sync cron +# (sync.yml) only when upstream actually moved — we fuzz on real upstream movement or an explicit +# dispatch, NOT on every commit to the mayhem branch. +# Three duration tiers: +# - 300s (5 min) manual dispatch / `/check` — a CONFIRMATION run that proves every target gets edges +# in Mayhem (run-health). This is the integrated gate; kept short so /check is fast. +# - 1200s (20 min) the weekly sync cron (workflow_call) — regular bug-finding as upstream moves. +# - long (4h+) a deliberate DEEP-RUN pass: scripts/integration/deep-run.sh, or +# gh workflow run mayhem.yml --ref mayhem -f duration=14400 +# Only the 5-min edges check gates `integrated`; 20m/4h are bug-finding, never required for integrated. +# Fill in the `mayhemfile` matrix below. Needs the MAYHEM_TOKEN secret. +name: Mayhem + +on: + workflow_dispatch: # manual / `/check` run (gh workflow run mayhem.yml --ref mayhem [-f duration=14400]) + inputs: + duration: + description: 'Fuzz seconds per target (default 300 = 5 min confirmation; e.g. 14400 = 4h deep run).' + type: string + required: false + default: '300' + workflow_call: # sync.yml calls this (only when upstream moved), passing the rebased SHA + inputs: + ref: + description: commit/branch to build (cron passes the freshly-rebased mayhem SHA) + type: string + required: false + default: '' + duration: # the weekly cron's regular bug-finding run: 20 min (deep-run requests longer) + description: fuzz seconds per target + type: string + required: false + default: '1200' + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build: + name: build commit image + runs-on: ubuntu-latest + permissions: + contents: read + packages: write # push the commit image to ghcr.io// + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + fetch-depth: 0 # FULL history — the Dockerfile COPYs .git into /mayhem, so the image + # must hold a real copy of the `mayhem` branch (upstream history + the + # additive integration commit). The default depth-1 shallow clone bakes + # in a single squashed-looking commit and breaks the grader / PATCH tier. + ref: ${{ inputs.ref || github.sha }} # build the cron's rebased SHA when called + + - name: Resolve build SHA + id: sha + run: echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" + + - name: Log in to ghcr.io + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Image metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: type=raw,value=commit-${{ steps.sha.outputs.sha }} # tag the exact built commit + + - name: Build and push (mayhem/Dockerfile) + uses: docker/build-push-action@v6 + with: + context: . + file: mayhem/Dockerfile + push: true + build-args: | + COMMIT=${{ steps.sha.outputs.sha }} + REPO_URL=${{ github.server_url }}/${{ github.repository }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + outputs: + image: ${{ steps.meta.outputs.tags }} + ref: ${{ steps.sha.outputs.sha }} + + mayhem: + needs: build + name: 'fuzz ${{ matrix.mayhemfile }}' + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + mayhemfile: + - mayhem/Mayhemfile_parse + - mayhem/Mayhemfile_asr + - mayhem/Mayhemfile_c + - mayhem/Mayhemfile_wat + - mayhem/Mayhemfile_wasm_x64 + - mayhem/Mayhemfile_cpp_python + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ needs.build.outputs.ref }} + - name: Start analysis for ${{ matrix.mayhemfile }} + uses: ForAllSecure/mcode-action@v1 + with: + mayhem-token: ${{ secrets.MAYHEM_TOKEN }} + args: --image ${{ needs.build.outputs.image }} --file ${{ matrix.mayhemfile }} --duration ${{ inputs.duration || '1200' }} diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml new file mode 100644 index 0000000000..3c52ccd215 --- /dev/null +++ b/.github/workflows/sync.yml @@ -0,0 +1,103 @@ +# Weekly sync. If upstream MOVED: reset the mirror branch to upstream, rebase the mayhem layer, +# build-verify, push, and (only then) run Mayhem on the new commit image. If it did NOT move: nothing. +# Set UPSTREAM below. (The mirror branch tracks upstream; the mayhem branch holds our additive edits.) +name: Sync with upstream + +on: + schedule: + - cron: '59 22 * * 6' + workflow_dispatch: # manual / external trigger (also how a backup orchestrator pokes it) + +permissions: + contents: write # push the mirror + mayhem + packages: write # the called Mayhem worker pushes the commit image to ghcr + issues: write # flag a rebase conflict / build break + +jobs: + sync: + runs-on: ubuntu-latest + outputs: + changed: ${{ steps.detect.outputs.changed }} + ref: ${{ steps.push.outputs.ref }} + env: + UPSTREAM: https://github.com/lcompilers/lpython.git # EDIT per repo + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Fetch upstream + run: | + git config user.name "mayhem-sync" + git config user.email "mayhem-sync@users.noreply.github.com" + git remote add upstream "$UPSTREAM" 2>/dev/null || git remote set-url upstream "$UPSTREAM" + git fetch --prune upstream + git fetch --prune origin + + - name: Detect upstream movement + id: detect + run: | + # mirror branch is named after upstream's default (master/main) so "Sync fork" works + DEF="$(git remote show upstream | sed -n 's/.*HEAD branch: //p')" + echo "DEF=$DEF" >> "$GITHUB_ENV" + if [ "$(git rev-list --count "origin/$DEF..upstream/$DEF")" -gt 0 ]; then + echo "changed=true" >> "$GITHUB_OUTPUT"; echo ">> upstream moved — syncing + fuzzing" + else + echo "changed=false" >> "$GITHUB_OUTPUT"; echo ">> upstream unchanged — nothing to do" + fi + + - name: Mirror upstream onto the mirror branch + if: steps.detect.outputs.changed == 'true' + run: | + # The mirror holds nothing of ours — reset, don't merge: upstream may rewrite history. + git checkout -B "$DEF" "upstream/$DEF" + git push --force origin "$DEF" + + - name: Rebase mayhem onto the mirror + id: rebase + if: steps.detect.outputs.changed == 'true' + run: | + git checkout -B mayhem origin/mayhem + if git rebase "$DEF"; then echo "ok=1" >> "$GITHUB_OUTPUT" + else git rebase --abort; echo "ok=0" >> "$GITHUB_OUTPUT"; fi + + - name: Flag conflict (needs manual fixup) and stop + # NB: guard on changed=='true' — a skipped rebase makes `ok` empty, and GitHub coerces + # '' == '0' to 0 == 0 (true), which would fire this spuriously on no-change weeks. + if: steps.detect.outputs.changed == 'true' && steps.rebase.outputs.ok == '0' + run: | + gh issue create --title "mayhem rebase conflict $(date -u +%F)" \ + --body "Weekly sync could not rebase \`mayhem\` onto upstream. Resolve by hand." || true + exit 1 + + - name: Build-verify the rebased mayhem branch + id: build + if: steps.rebase.outputs.ok == '1' + run: docker build -f mayhem/Dockerfile -t mayhem-verify . # a clean rebase can still break the build + + - name: Flag build break (upstream change broke the harness) and stop + if: steps.detect.outputs.changed == 'true' && failure() && steps.build.outcome == 'failure' + run: | + gh issue create --title "mayhem build broke after upstream sync $(date -u +%F)" \ + --body "\`mayhem\` rebased cleanly but \`docker build -f mayhem/Dockerfile\` failed — upstream likely changed the harnessed API/build. Fix mayhem/build.sh or the harness, then re-run. (mayhem was NOT pushed, so the last good image stands.)" || true + + - name: Push the rebased mayhem + id: push + if: steps.rebase.outputs.ok == '1' && steps.build.outcome == 'success' + run: | + git push --force-with-lease origin mayhem + echo "ref=$(git rev-parse mayhem)" >> "$GITHUB_OUTPUT" + + # Runs ONLY when upstream moved AND mayhem rebased+built — then build the commit image + fuzz (20 min). + # Calls the worker as a reusable workflow (no PAT, no GITHUB_TOKEN trigger problem). + fuzz: + needs: sync + if: needs.sync.outputs.changed == 'true' && needs.sync.outputs.ref != '' + permissions: + contents: read + packages: write + uses: ./.github/workflows/mayhem.yml + with: + ref: ${{ needs.sync.outputs.ref }} + secrets: inherit diff --git a/mayhem/.gitignore b/mayhem/.gitignore new file mode 100644 index 0000000000..758d905ebb --- /dev/null +++ b/mayhem/.gitignore @@ -0,0 +1,2 @@ +build-fuzz/ +build-test/ diff --git a/mayhem/Dockerfile b/mayhem/Dockerfile new file mode 100644 index 0000000000..8da9d5405a --- /dev/null +++ b/mayhem/Dockerfile @@ -0,0 +1,35 @@ +# syntax=docker/dockerfile:1.7 +# Commit image for lpython: builds on the org base (clang-19, libFuzzer, identities, build ENV). +FROM ghcr.io/savantenvs/base:20261023 + +ARG COMMIT +ARG REPO_URL +ARG SOURCE_DATE_EPOCH +ARG SANITIZER_FLAGS +ARG COVERAGE_FLAGS= +ARG MAYHEM_JOBS= + +# Generator tools for build0's steps (re2c tokenizer) and the static zlib the CMake build links. +USER root +RUN [ -n "$SOURCE_DATE_EPOCH" ] && repro-sources-list.sh || true ; \ + apt-get update && apt-get install -yq --no-install-recommends re2c zlib1g-dev \ + && rm -rf /var/lib/apt/lists/* +USER mayhem + +USER mayhem +WORKDIR /mayhem +COPY --chown=2000:2000 . /mayhem + +RUN if [ -x /opt/mayhem/ccache/prefill ]; then /opt/mayhem/ccache/prefill bash mayhem/build.sh; \ + else bash mayhem/build.sh; fi + +RUN if [ -f mayhem/test.sh ]; then bash mayhem/test.sh; else echo "no mayhem/test.sh — repo ships no functional tests"; fi + +LABEL org.opencontainers.image.source="${REPO_URL}" \ + org.opencontainers.image.revision="${COMMIT}" \ + rlenv.tier="commit" \ + rlenv.source_date_epoch="${SOURCE_DATE_EPOCH}" \ + rlenv.user="mayhem" + +USER mayhem +WORKDIR /mayhem diff --git a/mayhem/Dockerfile.dockerignore b/mayhem/Dockerfile.dockerignore new file mode 100644 index 0000000000..7cc8a470f0 --- /dev/null +++ b/mayhem/Dockerfile.dockerignore @@ -0,0 +1,2 @@ +mayhem/build-fuzz +mayhem/build-test diff --git a/mayhem/Mayhemfile_asr b/mayhem/Mayhemfile_asr new file mode 100644 index 0000000000..df265d4d7e --- /dev/null +++ b/mayhem/Mayhemfile_asr @@ -0,0 +1,12 @@ +project: savantenvs/lpython +target: asr + +cmds: + - cmd: /mayhem/fuzz_asr + libfuzzer: true + timeout: 10 + max_length: 16384 + +testsuite: + - https://$MAYHEM_DOMAIN/$MAYHEM_PROJECT/$MAYHEM_TARGET/testsuite.tar + - file://mayhem/asr/testsuite diff --git a/mayhem/Mayhemfile_c b/mayhem/Mayhemfile_c new file mode 100644 index 0000000000..8da35b2907 --- /dev/null +++ b/mayhem/Mayhemfile_c @@ -0,0 +1,12 @@ +project: savantenvs/lpython +target: c-backend + +cmds: + - cmd: /mayhem/fuzz_c + libfuzzer: true + timeout: 10 + max_length: 16384 + +testsuite: + - https://$MAYHEM_DOMAIN/$MAYHEM_PROJECT/$MAYHEM_TARGET/testsuite.tar + - file://mayhem/c-backend/testsuite diff --git a/mayhem/Mayhemfile_cpp_python b/mayhem/Mayhemfile_cpp_python new file mode 100644 index 0000000000..55ad8a1b08 --- /dev/null +++ b/mayhem/Mayhemfile_cpp_python @@ -0,0 +1,12 @@ +project: savantenvs/lpython +target: cpp-python + +cmds: + - cmd: /mayhem/fuzz_cpp_python + libfuzzer: true + timeout: 10 + max_length: 16384 + +testsuite: + - https://$MAYHEM_DOMAIN/$MAYHEM_PROJECT/$MAYHEM_TARGET/testsuite.tar + - file://mayhem/cpp-python/testsuite diff --git a/mayhem/Mayhemfile_parse b/mayhem/Mayhemfile_parse new file mode 100644 index 0000000000..b94feda512 --- /dev/null +++ b/mayhem/Mayhemfile_parse @@ -0,0 +1,12 @@ +project: savantenvs/lpython +target: parse + +cmds: + - cmd: /mayhem/fuzz_parse + libfuzzer: true + timeout: 10 + max_length: 16384 + +testsuite: + - https://$MAYHEM_DOMAIN/$MAYHEM_PROJECT/$MAYHEM_TARGET/testsuite.tar + - file://mayhem/parse/testsuite diff --git a/mayhem/Mayhemfile_wasm_x64 b/mayhem/Mayhemfile_wasm_x64 new file mode 100644 index 0000000000..5301cf4370 --- /dev/null +++ b/mayhem/Mayhemfile_wasm_x64 @@ -0,0 +1,12 @@ +project: savantenvs/lpython +target: wasm-x64 + +cmds: + - cmd: /mayhem/fuzz_wasm_x64 + libfuzzer: true + timeout: 10 + max_length: 16384 + +testsuite: + - https://$MAYHEM_DOMAIN/$MAYHEM_PROJECT/$MAYHEM_TARGET/testsuite.tar + - file://mayhem/wasm-x64/testsuite diff --git a/mayhem/Mayhemfile_wat b/mayhem/Mayhemfile_wat new file mode 100644 index 0000000000..6e2fcd68d4 --- /dev/null +++ b/mayhem/Mayhemfile_wat @@ -0,0 +1,12 @@ +project: savantenvs/lpython +target: wat + +cmds: + - cmd: /mayhem/fuzz_wat + libfuzzer: true + timeout: 10 + max_length: 16384 + +testsuite: + - https://$MAYHEM_DOMAIN/$MAYHEM_PROJECT/$MAYHEM_TARGET/testsuite.tar + - file://mayhem/wat/testsuite diff --git a/mayhem/asr/testsuite/array_01_decl.py b/mayhem/asr/testsuite/array_01_decl.py new file mode 100755 index 0000000000..153f7c9557 --- /dev/null +++ b/mayhem/asr/testsuite/array_01_decl.py @@ -0,0 +1,43 @@ +from lpython import i16, i32, i64, f32, f64, c32, c64 +from numpy import empty, int16, int32, int64, float32, float64, complex64, complex128 +from enum import Enum + +class ArraySizes(Enum): + SIZE_3: i32 = 3 + SIZE_10: i32 = 10 + +def accept_i16_array(xi16: i16[:]) -> i16: + xi16[2] = i16(32) + return xi16[2] + +def accept_i32_array(xi32: i32[:]) -> i32: + xi32[1] = 32 + return xi32[1] + +def accept_i64_array(xi64: i64[:]) -> i64: + xi64[1] = i64(64) + return xi64[1] + +def accept_f32_array(xf32: f32[:]) -> f32: + xf32[1] = f32(32.0) + return xf32[1] + +def accept_f64_array(xf64: f64[:]) -> f64: + xf64[0] = 64.0 + return xf64[0] + +def declare_arrays(): + ai16: i16[ArraySizes.SIZE_3.value] = empty(ArraySizes.SIZE_3.value, dtype=int16) + ai32: i32[ArraySizes.SIZE_3] = empty(ArraySizes.SIZE_3.value, dtype=int32) + ai64: i64[10] = empty(10, dtype=int64) + af32: f32[3] = empty(3, dtype=float32) + af64: f64[ArraySizes.SIZE_10] = empty(10, dtype=float64) + ac32: c32[ArraySizes.SIZE_3] = empty(3, dtype=complex64) + ac64: c64[10] = empty(10, dtype=complex128) + print(accept_i16_array(ai16)) + print(accept_i32_array(ai32)) + print(accept_i64_array(ai64)) + print(accept_f32_array(af32)) + print(accept_f64_array(af64)) + +declare_arrays() diff --git a/mayhem/asr/testsuite/array_02_decl.py b/mayhem/asr/testsuite/array_02_decl.py new file mode 100755 index 0000000000..1f5c07d76c --- /dev/null +++ b/mayhem/asr/testsuite/array_02_decl.py @@ -0,0 +1,29 @@ +from lpython import i32, i64, f32, f64, c32, c64 +from numpy import empty, int32, int64, float32, float64, complex64, complex128 + +def accept_multidim_i32_array(xi32: i32[:, :]) -> i32: + return xi32[0, 0] + +def accept_multidim_i64_array(xi64: i64[:, :, :]) -> i64: + return xi64[9, 9, 9] + +def accept_multidim_f32_array(xf32: f32[:]) -> f32: + return xf32[0] + +def accept_multidim_f64_array(xf64: f64[:, :]) -> f64: + return xf64[0, 1] + +def declare_arrays(): + ai32: i32[3, 3] = empty([3, 3], dtype=int32) + ai64: i64[10, 10, 10] = empty([10, 10, 10], dtype=int64) + af32: f32[3] = empty(3, dtype=float32) + af64: f64[10, 4] = empty([10, 4], dtype=float64) + ac32: c32[3, 5, 99] = empty([3, 5, 99], dtype=complex64) + ac64: c64[10, 13, 11, 16] = empty([10, 13, 11, 16], dtype=complex128) + print(accept_multidim_i32_array(ai32)) + print(accept_multidim_i64_array(ai64)) + print(accept_multidim_f32_array(af32)) + print(accept_multidim_f64_array(af64)) + + +declare_arrays() diff --git a/mayhem/asr/testsuite/assert1.py b/mayhem/asr/testsuite/assert1.py new file mode 100755 index 0000000000..bed46b99d9 --- /dev/null +++ b/mayhem/asr/testsuite/assert1.py @@ -0,0 +1,5 @@ +def test_assert(): + a: i32 + a = 5 + assert a == 5, 'a is not 5' + assert a != 10 diff --git a/mayhem/asr/testsuite/assign2.py b/mayhem/asr/testsuite/assign2.py new file mode 100755 index 0000000000..3742bcc9c6 --- /dev/null +++ b/mayhem/asr/testsuite/assign2.py @@ -0,0 +1,6 @@ +from lpython import i32, i64, f32, f64 + +i: i32 = 5 +i2: i64 = i64(53430903434) +f: f32 = f32(1.23456789) +f2: f64 = 1.2345678901234 diff --git a/mayhem/asr/testsuite/c_interop1.py b/mayhem/asr/testsuite/c_interop1.py new file mode 100755 index 0000000000..bc5c8f13a6 --- /dev/null +++ b/mayhem/asr/testsuite/c_interop1.py @@ -0,0 +1,32 @@ +from lpython import ccall, f32, f64, i32, i64 + +@ccall +def f(x: f64) -> f64: + pass + +@ccall +def g(a: f64, b: f32, c: i64, d: i32) -> None: + pass + +@ccallback +def h(x: f64) -> f64: + return x + 1.0 + +@ccallback +def l(a: f64, b: f32, c: i64, d: i32) -> None: + print("OK") + +def main0(): + i: f64 + x: f64 + x = 5.0 + i = f(x) + y: f32 + y = f32(5.4) + z: i64 + z = i64(3) + zz: i32 + zz = 2 + g(x, y, z, zz) + i = h(x) + l(x, y, z, zz) diff --git a/mayhem/asr/testsuite/callback_01.py b/mayhem/asr/testsuite/callback_01.py new file mode 100755 index 0000000000..e297c98630 --- /dev/null +++ b/mayhem/asr/testsuite/callback_01.py @@ -0,0 +1,26 @@ +from lpython import i32, Callable + + +def f(x: i32) -> i32: + return x + 1 + +def f2(x: i32) -> i32: + return x + 10 + +def f3(x: i32) -> i32: + return f(x) + f2(x) + + +def g(func: Callable[[i32], i32], arg: i32) -> i32: + ret: i32 + ret = func(arg) + return ret + + +def check(): + assert g(f, 10) == 11 + assert g(f2, 20) == 30 + assert g(f3, 5) == 21 + + +check() diff --git a/mayhem/asr/testsuite/complex1.py b/mayhem/asr/testsuite/complex1.py new file mode 100755 index 0000000000..5b16a6c7eb --- /dev/null +++ b/mayhem/asr/testsuite/complex1.py @@ -0,0 +1,38 @@ +from lpython import c32, c64 + +def test_complex(): + c: c32 + c1: c32 + c2: c32 + c3: c64 + b: bool + + # constant real or int as args + c = c32(complex()) + c = c32(complex(3.4)) + c = c32(complex(5., 4.3)) + c = c32(complex(1)) + c1 = c32(complex(3, 4)) + c2 = c32(complex(2, 4.5)) + c3 = complex(3., 4.) + b = c1 != c2 + b = c64(c1) == c3 + + # binary ops + c = c1 + c2 + c = c2 - c1 + c = c1 * c2 + c = c32(complex(1, 2) ** complex(3.34534, 4.8678678)) + c = c32(complex(1, 2) * complex(3, 4)) + c = c32(complex(4, 5) - complex(3, 4)) + + +def test(): + x: c64 + y: c64 + z: c32 + x = c64(2) + 3j + y = c64(5) + 5j + z = c32(x + y) + z = c32(x - y) + z = c32(c64(2) * x) diff --git a/mayhem/asr/testsuite/dictionary1.py b/mayhem/asr/testsuite/dictionary1.py new file mode 100755 index 0000000000..b50e326d5a --- /dev/null +++ b/mayhem/asr/testsuite/dictionary1.py @@ -0,0 +1,41 @@ +def test_Dict(): + x: dict[i32, i32] + x = {1: 2, 3: 4} + # x = {1: "2", "3": 4} -> semantic error + + y: dict[str, i32] + y = {"a": -1, "b": -2} + + z: i32 + z = y["a"] + z = y["b"] + z = x[1] + + +def test_dict_insert(): + y: dict[str, i32] + y = {"a": -1, "b": -2} + y["c"] = -3 + + +def test_dict_get(): + y: dict[str, i32] + y = {"a": -1, "b": -2} + x: i32 + x = y.get("a") + x = y.get("a", 0) + + +def test_dict_pop(): + y: dict[str, i32] + y = {"a": 1, "b": 2} + x: i32 + x = y.pop("a") + + +def f(x: InOut[dict[i32, i32]]): + x[2] = 4 + +def test_issue_204(): + x: dict[i32, i32] + f(x) diff --git a/mayhem/asr/testsuite/elemental_01.py b/mayhem/asr/testsuite/elemental_01.py new file mode 100755 index 0000000000..0a2787c7ed --- /dev/null +++ b/mayhem/asr/testsuite/elemental_01.py @@ -0,0 +1,165 @@ +from lpython import i32, f64, f32 +from numpy import empty, sin, cos, reshape, int32, float32, float64 + +def verify1d(array: f32[:], result: f32[:], size: i32): + i: i32 + eps: f32 + eps = f32(1e-6) + + for i in range(size): + assert abs(sin(sin(array[i])) - result[i]) <= eps + +def verifynd(array: f64[:, :, :], result: f64[:, :, :], size1: i32, size2: i32, size3: i32): + i: i32 + j: i32 + k: i32 + eps: f64 + eps = 1e-12 + + for i in range(size1): + for j in range(size2): + for k in range(size3): + assert abs(sin(array[i, j, k])**2.0 - result[i, j, k]) <= eps + +def verify2d(array: f64[:, :], result: f64[:, :], size1: i32, size2: i32): + i: i32 + j: i32 + eps: f64 + eps = 1e-12 + + for i in range(size1): + for j in range(size2): + assert abs(cos(array[i, j])**2.0 - result[i, j]) <= eps + + +def verify1d_sum(array_a: f64[:], array_b: f64[:], result: f64[:], size: i32): + i: i32 + eps: f64 + eps = 1e-9 + + for i in range(size): + assert abs(array_a[i]**2.0 + 5.0*array_b[i]**3.0 - result[i]) <= eps + + +def verify1d_mul(array_a: f64[:], array_b: f64[:], result: f64[:], size: i32): + i: i32 + eps: f64 + eps = 1e-5 + + for i in range(size): + assert abs(array_a[i]**2.0 * 5.0*array_b[i]**3.0 - result[i]) <= eps + + +def elemental_sum(): + i: i32 + j: i32 + k: i32 + + array_a: f64[100] = empty(100, dtype=float64) + array_b: f64[100] = empty(100, dtype=float64) + array_c: f64[100] = empty(100, dtype=float64) + + for i in range(100): + array_a[i] = float(i) + + for j in range(100): + array_b[j] = float(j+5) + + array_c = array_a**2.0 + 5.0*array_b**3.0 + verify1d_sum(array_a, array_b, array_c, 100) + + +def elemental_mul(): + i: i32 + j: i32 + k: i32 + + array_a: f64[100] = empty(100, dtype=float64) + array_b: f64[100] = empty(100, dtype=float64) + array_c: f64[100] = empty(100, dtype=float64) + + for i in range(100): + array_a[i] = float(i) + + for j in range(100): + array_b[j] = float(j+5) + + array_c = array_a**2.0 * 5.0*array_b**3.0 + verify1d_mul(array_a, array_b, array_c, 100) + + +def elemental_sin(): + i: i32 + j: i32 + k: i32 + + array1d: f32[256] = empty(256, dtype=float32) + sin1d: f32[256] = empty(256, dtype=float32) + + for i in range(256): + array1d[i] = f32(i) + + sin1d = sin(sin(array1d)) + + verify1d(array1d, sin1d, 256) + + arraynd: f64[200, 64, 16] = empty((200, 64, 16), dtype=float64) + sinnd: f64[200, 64, 16] = empty((200, 64, 16), dtype=float64) + + for i in range(200): + for j in range(64): + for k in range(16): + arraynd[i, j, k] = float(i + j + k) + + sinnd = sin(arraynd)**2.0 + + verifynd(arraynd, sinnd, 200, 64, 16) + +def elemental_cos(): + i: i32 + j: i32 + + array2d: f64[256, 64] = empty((256, 64), dtype=float64) + cos2d: f64[256, 64] = empty((256, 64), dtype=float64) + + for i in range(256): + for j in range(64): + array2d[i, j] = float(i + j) + + cos2d = cos(array2d)**2.0 + + verify2d(array2d, cos2d, 256, 64) + +def elemental_trig_identity(): + i: i32 + j: i32 + k: i32 + l: i32 + eps: f32 + eps = f32(1e-6) + + arraynd: f32[64, 32, 8, 4] = empty((64, 32, 8, 4), dtype=float32) + observed: f32[64, 32, 8, 4] = empty((64, 32, 8, 4), dtype=float32) + observed1d: f32[65536] = empty(65536, dtype=float32) + + for i in range(64): + for j in range(32): + for k in range(8): + for l in range(4): + arraynd[i, j, k, l] = f32(i + j + k + l) + + observed = sin(arraynd)**f32(2) + cos(arraynd)**f32(2) + + newshape: i32[1] = empty(1, dtype=int32) + newshape[0] = 65536 + observed1d = reshape(observed, newshape) + + for i in range(65536): + assert abs(observed1d[i] - f32(1.0)) <= eps + + +elemental_sin() +elemental_cos() +elemental_trig_identity() +elemental_sum() +elemental_mul() \ No newline at end of file diff --git a/mayhem/asr/testsuite/expr1.py b/mayhem/asr/testsuite/expr1.py new file mode 100755 index 0000000000..4c889a09b8 --- /dev/null +++ b/mayhem/asr/testsuite/expr1.py @@ -0,0 +1,15 @@ +def test_namedexpr(): + a: i32 + x: i32 + y: i32 + + # check 1 + x = (y := 0) + + # check 2 + if a := ord('3'): + x = 1 + + # check 3 + while a := 1: + y = 1 diff --git a/mayhem/asr/testsuite/expr10.py b/mayhem/asr/testsuite/expr10.py new file mode 100755 index 0000000000..4838f3dc19 --- /dev/null +++ b/mayhem/asr/testsuite/expr10.py @@ -0,0 +1,31 @@ +from lpython import i32, f32, c32 + +def test_UnaryOp(): + a: i32 + a = +4 + a = -500 + a = ~5 + b: bool + b = not 5 + b = not -1 + b = not 0 + + f: f32 + f = +f32(1.0) + f = -f32(183745.534) + + b1: bool + b2: bool + b3: bool + b1 = True + b2 = not False + b3 = not b2 + a = +True + a = -False + a = ~True + + c: c32 + c = +c32(complex(1, 2)) + c = -c32(complex(3, 65.0)) + b1 = not complex(3, 4) + b2 = not complex(0, 0) diff --git a/mayhem/asr/testsuite/expr12.py b/mayhem/asr/testsuite/expr12.py new file mode 100755 index 0000000000..211b3e1cb4 --- /dev/null +++ b/mayhem/asr/testsuite/expr12.py @@ -0,0 +1,13 @@ +def test(a: i32, b: i32) -> i32: + return a**b + +def check() -> i32: + a: i32 + a = test(2, 2) + return a + +def main0(): + x: i32 + x = check() + +main0() diff --git a/mayhem/asr/testsuite/expr13.py b/mayhem/asr/testsuite/expr13.py new file mode 100755 index 0000000000..48a3984abf --- /dev/null +++ b/mayhem/asr/testsuite/expr13.py @@ -0,0 +1,23 @@ +def test_Compare(): + a: bool + a = 5 > 4 + a = 5 <= 4 + a = 5 < 4 + a = 5.6 >= 5.59999 + a = 3.3 == 3.3 + a = 3.3 != 3.4 + a = complex(3, 4) == complex(3., 4.) + + # string comparison + a = "abc" > "abd" + a = "" < "s" + a = "-abs" >= "abs" + a = "abcd" <= "abcde" + a = "abc" == "abc" + a = "abc" != "abd" + a = "" == "+" + + a = True > False + a = True == True + a = False != True + a = False >= True diff --git a/mayhem/asr/testsuite/expr2.py b/mayhem/asr/testsuite/expr2.py new file mode 100755 index 0000000000..b26a96cc6e --- /dev/null +++ b/mayhem/asr/testsuite/expr2.py @@ -0,0 +1,11 @@ +def test_boolOp(): + a: bool + b: bool + a = False + b = True + a = a and b + b = a or True + a = a or b + a = a and b == b + a = a and b != b + a = b or b diff --git a/mayhem/asr/testsuite/expr4.py b/mayhem/asr/testsuite/expr4.py new file mode 100755 index 0000000000..030b93569e --- /dev/null +++ b/mayhem/asr/testsuite/expr4.py @@ -0,0 +1,6 @@ +def test_del(): + a: i32 + b: i32 + a = 4 + b = 20 + del a, b diff --git a/mayhem/asr/testsuite/expr6.py b/mayhem/asr/testsuite/expr6.py new file mode 100755 index 0000000000..a7534d4a33 --- /dev/null +++ b/mayhem/asr/testsuite/expr6.py @@ -0,0 +1,7 @@ +def test_ifexp(): + a: i32 + b: i32 + c: bool + a = 2 + b = 6 if a == 2 else 8 + c = True if b > 5 else False diff --git a/mayhem/asr/testsuite/expr7.py b/mayhem/asr/testsuite/expr7.py new file mode 100755 index 0000000000..58125ba341 --- /dev/null +++ b/mayhem/asr/testsuite/expr7.py @@ -0,0 +1,16 @@ +from lpython import i32 +def test_pow(): + a: i32 + a = i32(pow(2, 2)) + +def test_pow_1(a: i32, b: i32) -> i32: + res: i32 + res = i32(pow(a, b)) + return res + +def main0(): + test_pow() + c: i32 + c = test_pow_1(1, 2) + +main0() diff --git a/mayhem/asr/testsuite/expr8.py b/mayhem/asr/testsuite/expr8.py new file mode 100755 index 0000000000..ac3bcc7db4 --- /dev/null +++ b/mayhem/asr/testsuite/expr8.py @@ -0,0 +1,23 @@ +from lpython import i32, f32 + +def test_binop(): + x: i32 + x2: f32 + x = 2**3 + x2 = f32(2.0**3.5) + x = 54 - 100 + x2 = f32((3.454 - 765.43) + 534.6) + x2 = f32(5346.565 * 3.45) + x2 = f32(5346.565 ** 3.45) + + # binop on logical type + x = True + True + x = True - False + x = True * False + x = True ** False + b1: bool + b2: bool + b1 = True + b2 = False + x = i32(b1//b1) + x = b1**b2 diff --git a/mayhem/asr/testsuite/expr_01.py b/mayhem/asr/testsuite/expr_01.py new file mode 100755 index 0000000000..3bd2e6b139 --- /dev/null +++ b/mayhem/asr/testsuite/expr_01.py @@ -0,0 +1,28 @@ +from lpython import inline, i32 + +@inline +def add(x: i32, y: i32) -> i32: + return x + y + +@inline +def and_op(x: i32, y: i32) -> i32: + return x & y + +def main0(): + x: i32 + y: i32 + z: i32 + x = (2+3)*5 + y = add(x, 2)*2 + assert x == 25 + assert y == 54 + + z = and_op(x, y) + assert z == 16 + + +main0() + +# Not implemented yet in LPython: +#if __name__ == "__main__": +# main() diff --git a/mayhem/asr/testsuite/expr_05.py b/mayhem/asr/testsuite/expr_05.py new file mode 100755 index 0000000000..08748469b7 --- /dev/null +++ b/mayhem/asr/testsuite/expr_05.py @@ -0,0 +1,89 @@ +from lpython import i32, i64, f64, u16 + +def test_multiply(a: i32, b: i32) -> i32: + return a*b + +def test_mod(a: i32, b: i32) -> i32: + return a%b + +def main0(): + a: i32 + b: i32 + a = 10 + b = -5 + eps: f64 + eps = 1e-12 + assert test_multiply(a, b) == -50 + i: i64 + i = i64(1) + i += int(1) + assert i == i64(2) + a = 2 + b = 5 + assert test_mod(a, b) == 2 + assert test_mod(23, 3) == 2 + a = 123282374 + b = 32771 + assert test_mod(a, b) == 30643 + a = -5345 + b = -534 + assert a % b == -5 + a = -123282374 + b = 32771 + assert test_mod(a, b) == 2128 + + assert 10 | 4 == 14 + assert -105346 | -32771 == -32769 + assert 10 & 4 == 0 + assert -105346 & -32771 == -105348 + assert 10 ^ 4 == 14 + assert -105346 ^ -32771 == 72579 + assert 10 >> 1 == 5 + assert 5 << 1 == 10 + i1: i32 = 10 + i2: i32 = 4 + assert i1 << i2 == 160 + assert i1 >> i2 == 0 + assert i1 & i2 == 0 + assert i1 | i2 == 14 + assert i1 ^ i2 == 14 + assert -i1 ^ -i2 == 10 + i3: i32 = 432534534 + i4: i32 = -4325 + assert i3 | i4 == -225 + assert i4 >> 3 == -541 + assert -i3 & i4 == -432534758 + assert -i3 ^ i4 == 432530657 + a = 10 + a |= 4 + assert a == 14 + a ^= 3 + assert a == 13 + b = 10 + a %= b + assert a == 3 + b = 4 + a <<= b + assert a == 48 + a >>= 1 + assert a == 24 + a &= b + assert a == 0 + b **= 4 + assert b == 256 + # Test Issue 1562 + assert ((-8)%3) == 1 + assert ((8)%-3) == -1 + assert (-8%-3) == -2 + assert abs((11.0%-3.0) - (-1.0)) < eps + assert abs((-11.0%3.0) - (1.0)) < eps + + # Test issue 1869 and 1870 + a1: u16 = u16(10) + b1: u16 = u16(3) + c1: u16 = a1 % b1 + assert c1 == u16(1) + c1 = a1 // b1 + assert c1 == u16(3) + +main0() diff --git a/mayhem/asr/testsuite/expr_07.py b/mayhem/asr/testsuite/expr_07.py new file mode 100755 index 0000000000..788fc41b58 --- /dev/null +++ b/mayhem/asr/testsuite/expr_07.py @@ -0,0 +1,29 @@ +from lpython import i32 + +def g(x: i32): + print(x) + +x: i32 = 7 + +def f(): + a: i32 = 5 + x: i32 = 3 + x = 5 + b: i32 = x + 1 + print(a, b) + assert b == 6 + g(a*b + 3) + + +def bool_to_str(): + var: bool + var = True + print(str(True)) + assert str(var) == "True" + var = False + assert str(var) == "False" + assert str(True) == "True" + + +f() +bool_to_str() diff --git a/mayhem/asr/testsuite/expr_09.py b/mayhem/asr/testsuite/expr_09.py new file mode 100755 index 0000000000..a64e5eb35f --- /dev/null +++ b/mayhem/asr/testsuite/expr_09.py @@ -0,0 +1,48 @@ +from lpython import i32, f64, f32 + +def main0(): + i1: i32 = 10 + i2: i32 = 4 + i1 = 3 + i2 = 5 + print(-i1 ^ -i2) + assert -i1 ^ -i2 == 6 + + +def test_multiple_assign_1(): + a: i32; b: i32; c: i32 + d: f64; e: f64; g: i32 + g = 5 + d = e = f64(g) + 1.0 + a = b = c = 10 + assert a == b + assert b == c + assert a == 10 + x: f64; y: f64 + x = y = 23.0 + assert abs(x - 23.0) < 1e-6 + assert abs(y - 23.0) < 1e-12 + assert abs(e - 6.0) < 1e-6 + assert abs(d - 6.0) < 1e-12 + i: list[f64]; j: list[f64]; k: list[f64] = [] + g = 0 + for g in range(10): + k.append(f64(g)*2.0 + 5.0) + i = j = k + for g in range(10): + assert abs(i[g] - j[g]) < 1e-12 + assert abs(i[g] - k[g]) < 1e-12 + assert abs(f64(g)*2.0 + 5.0 - k[g]) < 1e-12 + + +def test_issue_928(): + a: i32; b: i32; c: tuple[i32, i32] + a, b = c = 2, 1 + assert a == 2 + assert b == 1 + assert c[0] == a and c[1] == b + + +test_multiple_assign_1() +test_issue_928() +main0() diff --git a/mayhem/asr/testsuite/expr_10.py b/mayhem/asr/testsuite/expr_10.py new file mode 100755 index 0000000000..28a715f3e0 --- /dev/null +++ b/mayhem/asr/testsuite/expr_10.py @@ -0,0 +1,16 @@ +from lpython import i32 + +def g() -> i32: + return 5 + +def gsubrout(x: i32): + print(x) + +def test_fn1(): + i: i32 = g() + j: i32 + j = g() + g() + gsubrout(i) + +test_fn1() diff --git a/mayhem/asr/testsuite/expr_14.py b/mayhem/asr/testsuite/expr_14.py new file mode 100755 index 0000000000..5b22fee92c --- /dev/null +++ b/mayhem/asr/testsuite/expr_14.py @@ -0,0 +1,41 @@ +from lpython import i32, i64, f32, f64, c32, c64 + +def test_divide(): + a1: i32; a2: i32; a3: f64; + b1: i64; b2: i64; b3: f64; + c1: f32; c2: f32; c3: f32; + d1: f64; d2: f64; d3: f64; + e1: c32; e2: c32; e3: c32; + f1: c64; f2: c64; f3: c64; + + a1 = 1 + a2 = 9 + a3 = a2/a1 + assert abs(a3 - 9.0) <= 1e-12 + + b1 = i64(2) + b2 = i64(10) + b3 = b2/b1 + assert abs(b3 - 5.0) <= 1e-12 + + c1 = f32(3.0) + c2 = f32(11.0) + c3 = c2/c1 + assert abs(c3 - f32(3.666666)) <= f32(1e-6) + + d1 = 4.0 + d2 = 12.0 + d3 = d2/d1 + assert abs(d3 - 3.0) <= 1e-12 + + e1 = c32(5) + c32(6j) + e2 = c32(13) + c32(14j) + e3 = e2/e1 + assert abs(e3 - c32(2.442622950819672) + c32(0.13114754098360643j)) <= f32(1e-6) + + f1 = c64(7) + 8j + f2 = c64(15) + 16j + f3 = f2/f1 + assert abs(f3 - c64(2.061946902654867) + 0.07079646017699115j) <= 1e-6 + +test_divide() diff --git a/mayhem/asr/testsuite/func_inline_01.py b/mayhem/asr/testsuite/func_inline_01.py new file mode 100755 index 0000000000..26173b52bd --- /dev/null +++ b/mayhem/asr/testsuite/func_inline_01.py @@ -0,0 +1,16 @@ +from lpython import i64 + +def fib(n: i64) -> i64: + if n < i64(2): + return n + return fib(n - i64(1)) + fib(n - i64(2)) + +def main(): + ans: i64 + x: i64 + x = i64(40) + ans = fib(x) + print(ans) + assert ans == i64(102334155) + +main() diff --git a/mayhem/asr/testsuite/global_scope1.py b/mayhem/asr/testsuite/global_scope1.py new file mode 100755 index 0000000000..594b43caa7 --- /dev/null +++ b/mayhem/asr/testsuite/global_scope1.py @@ -0,0 +1,2 @@ +i: i32 +i = 5 diff --git a/mayhem/asr/testsuite/global_syms_01.py b/mayhem/asr/testsuite/global_syms_01.py new file mode 100755 index 0000000000..fbf9cbb97a --- /dev/null +++ b/mayhem/asr/testsuite/global_syms_01.py @@ -0,0 +1,12 @@ +from lpython import i32 + +x: list[i32] +x = [1, 2] +i: i32 +i = x[0] + +def test_global_symbols(): + assert i == 1 + assert x[1] == 2 + +test_global_symbols() diff --git a/mayhem/asr/testsuite/intent_01.py b/mayhem/asr/testsuite/intent_01.py new file mode 100755 index 0000000000..b3552079c0 --- /dev/null +++ b/mayhem/asr/testsuite/intent_01.py @@ -0,0 +1,6 @@ +@dataclass +class Foo: + p: i32 + +def f(x: i32, y: In[f64], z: InOut[list[u32]], w: Out[Foo[5]]): + pass diff --git a/mayhem/asr/testsuite/list1.py b/mayhem/asr/testsuite/list1.py new file mode 100755 index 0000000000..62a71cd9ae --- /dev/null +++ b/mayhem/asr/testsuite/list1.py @@ -0,0 +1,33 @@ +def test_List(): + a: list[i32] + a = [1, 2, 3] + a = [-3, -2, -1] + # a = [-2, -1, 0.45] -> semantic error + + b: list[str] + b = ["a", "b", "c"] + + c: list[list[i32]] + c = [[1, 2, 3], [4, 5, 6]] + + d: i32 + d = a[2] + + # ragged list + e: list[list[str]] + e = [['a', 'b', 'c'], ['d', 'e']] + + a.append(10) + a.remove(1) + a.insert(2, 13) + a = a[0:2] + d = a.pop() + d = a.pop(2) + a += [4, 5] + a = [6, 7] + a + + a11: list[i32] + b11: list[i32] + a11 = [1, 2] + b11 = [3, 4] + assert a11 == b11 diff --git a/mayhem/build.sh b/mayhem/build.sh new file mode 100755 index 0000000000..ef908d486b --- /dev/null +++ b/mayhem/build.sh @@ -0,0 +1,90 @@ +#!/usr/bin/env bash +# +# mayhem/build.sh — build lpython's fuzz harnesses (ASan+UBSan) and a normal-flags lpython for +# the reference test suite (mayhem/test.sh). Runs in /mayhem as `mayhem`; re-runnable and offline. +set -euo pipefail +[ -n "${SOURCE_DATE_EPOCH:-}" ] || unset SOURCE_DATE_EPOCH +: "${SANITIZER_FLAGS=-fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer}" +: "${DEBUG_FLAGS:=-g -gdwarf-3}" +: "${CC:=clang}" ; : "${CXX:=clang++}" ; : "${LIB_FUZZING_ENGINE:=-fsanitize=fuzzer}" +# Default parallelism is bounded by memory as well as cores: sanitized compiles of the large +# semantics/codegen units take ~2.5 GB each, and 16 at once OOM a 12 GB container. +if [ -z "${MAYHEM_JOBS:-}" ]; then + mem=$(cat /sys/fs/cgroup/memory.max 2>/dev/null || echo max) + [ "$mem" = max ] && mem=$(( $(awk '/^MemTotal/{print $2}' /proc/meminfo) * 1024 )) + MAYHEM_JOBS=$(( mem / (2560 * 1024 * 1024) )) + [ "$MAYHEM_JOBS" -ge 1 ] || MAYHEM_JOBS=1 + [ "$MAYHEM_JOBS" -le "$(nproc)" ] || MAYHEM_JOBS=$(nproc) +fi +: "${COVERAGE_FLAGS=}" +: "${SRC:=/mayhem}" +export SANITIZER_FLAGS DEBUG_FLAGS CC CXX LIB_FUZZING_ENGINE MAYHEM_JOBS COVERAGE_FLAGS +cd "$SRC" + +# 0) Generated sources (what upstream's build0.sh does). Always regenerate so an edit to the +# grammar / ASDL is picked up. The version comes from `git describe`; fall back when no tag +# is reachable (shallow or tagless checkout). +# Keep the old mtime of every generated file whose content did not change, so a re-run is +# incremental instead of a full rebuild (the generators rewrite all their outputs every time). +PREV=mayhem/build-fuzz/.gen-prev +rm -rf "$PREV"; mkdir -p "$PREV" +gen_candidates() { + ls lp_version src/runtime/python_ast.py src/lpython/*.h src/lpython/parser/* \ + libasr/src/libasr/*.h libasr/src/libasr/pass/*.h 2>/dev/null || true +} +gen_candidates | while read -r f; do if [ -f "$f" ]; then cp -p --parents "$f" "$PREV/"; fi; done +ci/version.sh 2>/dev/null || echo "0.0.0-mayhem" > lp_version +python grammar/asdl_py.py +python libasr/src/libasr/asdl_cpp.py grammar/Python.asdl src/lpython/python_ast.h +python libasr/src/libasr/asdl_cpp.py libasr/src/libasr/ASR.asdl libasr/src/libasr/asr.h +python libasr/src/libasr/wasm_instructions_visitor.py +python libasr/src/libasr/intrinsic_func_registry_util_gen.py +(cd src/lpython/parser && re2c -W -b tokenizer.re -o tokenizer.cpp) +(cd src/lpython/parser && bison -Wall -d -r all parser.yy) +python - <<'PY' +file = 'src/lpython/parser/parser.tab.cc' +text = open(file).read() +open(file, 'w').write('[[maybe_unused]] int yynerrs'.join(text.split('int yynerrs'))) +PY +gen_candidates | while read -r f; do + if [ -f "$PREV/$f" ] && cmp -s "$f" "$PREV/$f"; then touch -r "$PREV/$f" "$f"; fi +done +rm -rf "$PREV" +common_cmake=(-DCMAKE_BUILD_TYPE=Release -DWITH_LLVM=no -DWITH_STACKTRACE=no + -DWITH_RUNTIME_STACKTRACE=no -DWITH_LCOMPILERS_FAST_ALLOC=no) + +# 1) Sanitized library build (the fuzzed code is instrumented). +# UBSan's `bool` check is relaxed: every generated AST/ASR visitor ends with +# `if ((bool&)x) { }` ("suppress unused warning"), which loads a struct byte as bool and fires +# on the first input. ASan and the rest of UBSan stay on and halting. +UBSAN_RELAX="-fno-sanitize=bool" +FUZZ_FLAGS="$SANITIZER_FLAGS $UBSAN_RELAX $DEBUG_FLAGS -O1 -fsanitize=fuzzer-no-link" +cmake -S . -B mayhem/build-fuzz "${common_cmake[@]}" \ + -DCMAKE_C_FLAGS="$FUZZ_FLAGS" -DCMAKE_CXX_FLAGS="$FUZZ_FLAGS" \ + -DCMAKE_C_FLAGS_RELEASE="" -DCMAKE_CXX_FLAGS_RELEASE="" +cmake --build mayhem/build-fuzz -j"$MAYHEM_JOBS" --target lpython_lib asr lpython_runtime_static + +B=mayhem/build-fuzz +LIBS=("$B/src/lpython/liblpython_lib.a" "$B/libasr/libasr.a" + "$(find "$B/src/runtime/legacy" -name 'liblpython_runtime_static.a' | head -1)") +ZLIB=/usr/lib/x86_64-linux-gnu/libz.a +INC=(-Isrc -I"$B/src" -Ilibasr/src -I"$B/libasr/..") +CXXH=(-std=c++17 -DHAVE_WHEREAMI=1 -DHAVE_ZLIB=1) + +$CXX $SANITIZER_FLAGS $DEBUG_FLAGS -c mayhem/lsan_off.cc -o $B/lsan_off.o +$CC $SANITIZER_FLAGS $DEBUG_FLAGS -c "$STANDALONE_FUZZ_MAIN" -o $B/standalone_main.o + +for h in parse asr c wat wasm_x64 cpp_python; do + $CXX $SANITIZER_FLAGS $UBSAN_RELAX $DEBUG_FLAGS -O1 "${CXXH[@]}" "${INC[@]}" \ + -c "mayhem/harnesses/fuzz_$h.cpp" -o "$B/fuzz_$h.o" + $CXX $SANITIZER_FLAGS $DEBUG_FLAGS $LIB_FUZZING_ENGINE "$B/fuzz_$h.o" $B/lsan_off.o \ + "${LIBS[@]}" "$ZLIB" -ldl -lpthread -o "$SRC/fuzz_$h" + $CXX $SANITIZER_FLAGS $DEBUG_FLAGS "$B/fuzz_$h.o" $B/standalone_main.o $B/lsan_off.o \ + "${LIBS[@]}" "$ZLIB" -ldl -lpthread -o "$SRC/fuzz_$h-standalone" +done + +# 2) Normal-flags build of the lpython CLI for the reference test suite (run_tests.py). +cmake -S . -B mayhem/build-test "${common_cmake[@]}" \ + -DCMAKE_C_FLAGS="$COVERAGE_FLAGS" -DCMAKE_CXX_FLAGS="$COVERAGE_FLAGS" \ + -DCMAKE_C_FLAGS_RELEASE="-O1 -DNDEBUG" -DCMAKE_CXX_FLAGS_RELEASE="-O1 -DNDEBUG" +cmake --build mayhem/build-test -j"$MAYHEM_JOBS" --target lpython diff --git a/mayhem/c-backend/testsuite/c_interop1.py b/mayhem/c-backend/testsuite/c_interop1.py new file mode 100755 index 0000000000..bc5c8f13a6 --- /dev/null +++ b/mayhem/c-backend/testsuite/c_interop1.py @@ -0,0 +1,32 @@ +from lpython import ccall, f32, f64, i32, i64 + +@ccall +def f(x: f64) -> f64: + pass + +@ccall +def g(a: f64, b: f32, c: i64, d: i32) -> None: + pass + +@ccallback +def h(x: f64) -> f64: + return x + 1.0 + +@ccallback +def l(a: f64, b: f32, c: i64, d: i32) -> None: + print("OK") + +def main0(): + i: f64 + x: f64 + x = 5.0 + i = f(x) + y: f32 + y = f32(5.4) + z: i64 + z = i64(3) + zz: i32 + zz = 2 + g(x, y, z, zz) + i = h(x) + l(x, y, z, zz) diff --git a/mayhem/c-backend/testsuite/expr7.py b/mayhem/c-backend/testsuite/expr7.py new file mode 100755 index 0000000000..58125ba341 --- /dev/null +++ b/mayhem/c-backend/testsuite/expr7.py @@ -0,0 +1,16 @@ +from lpython import i32 +def test_pow(): + a: i32 + a = i32(pow(2, 2)) + +def test_pow_1(a: i32, b: i32) -> i32: + res: i32 + res = i32(pow(a, b)) + return res + +def main0(): + test_pow() + c: i32 + c = test_pow_1(1, 2) + +main0() diff --git a/mayhem/c-backend/testsuite/expr_01.py b/mayhem/c-backend/testsuite/expr_01.py new file mode 100755 index 0000000000..3bd2e6b139 --- /dev/null +++ b/mayhem/c-backend/testsuite/expr_01.py @@ -0,0 +1,28 @@ +from lpython import inline, i32 + +@inline +def add(x: i32, y: i32) -> i32: + return x + y + +@inline +def and_op(x: i32, y: i32) -> i32: + return x & y + +def main0(): + x: i32 + y: i32 + z: i32 + x = (2+3)*5 + y = add(x, 2)*2 + assert x == 25 + assert y == 54 + + z = and_op(x, y) + assert z == 16 + + +main0() + +# Not implemented yet in LPython: +#if __name__ == "__main__": +# main() diff --git a/mayhem/c-backend/testsuite/expr_11.py b/mayhem/c-backend/testsuite/expr_11.py new file mode 100755 index 0000000000..1e27e821b5 --- /dev/null +++ b/mayhem/c-backend/testsuite/expr_11.py @@ -0,0 +1,11 @@ +from lpython import i32 + +def f(): + i: i32 + i = 3 + b: bool + b = bool(i) + assert b + print(b) + +f() diff --git a/mayhem/c-backend/testsuite/func_static_01.py b/mayhem/c-backend/testsuite/func_static_01.py new file mode 100755 index 0000000000..c87377f36e --- /dev/null +++ b/mayhem/c-backend/testsuite/func_static_01.py @@ -0,0 +1,15 @@ +from lpython import i64, static + +@static +def fib(n: i64) -> i64: + if n < i64(2): + return n + return fib(n - i64(1)) + fib(n - i64(2)) + +def main0(): + ans: i64; x: i64 + x = i64(5) + ans = fib(x) + assert ans == i64(5) + +main0() diff --git a/mayhem/c-backend/testsuite/import_order_01.py b/mayhem/c-backend/testsuite/import_order_01.py new file mode 100755 index 0000000000..0d8724338d --- /dev/null +++ b/mayhem/c-backend/testsuite/import_order_01.py @@ -0,0 +1,7 @@ +from import_order_01b import f +from lpython import i32, ccallback + +@ccallback +def main1(): + a: i32 = f() + print(a) diff --git a/mayhem/c-backend/testsuite/loop4.py b/mayhem/c-backend/testsuite/loop4.py new file mode 100755 index 0000000000..11db8c5a46 --- /dev/null +++ b/mayhem/c-backend/testsuite/loop4.py @@ -0,0 +1,11 @@ +def test_for(): + i: i32 + for i in range(0, 10): + if i == 0: + continue + if i > 5: + break + if i == 3: + quit() + +test_for() diff --git a/mayhem/c-backend/testsuite/test_import_02.py b/mayhem/c-backend/testsuite/test_import_02.py new file mode 100755 index 0000000000..5accbef42d --- /dev/null +++ b/mayhem/c-backend/testsuite/test_import_02.py @@ -0,0 +1,13 @@ +from test_import.import_module_01 import multiply, e +from test_import.import_module_02 import add, μ + + +def f(): + print(e) + print(μ) + print(add(10, 20)) + print(multiply(10, 20)) + assert add(10, 20) == 30 + assert multiply(10, 20) == 200 + +f() diff --git a/mayhem/c-backend/testsuite/test_issue_518.py b/mayhem/c-backend/testsuite/test_issue_518.py new file mode 100755 index 0000000000..755719a27b --- /dev/null +++ b/mayhem/c-backend/testsuite/test_issue_518.py @@ -0,0 +1,22 @@ +from lpython import i64 + +def fib(n: i64) -> i64: + if n < i64(2): + return n + else: + return fib(n - i64(1)) + fib(n - i64(2)) + +def main0(): + ans: i64 + ans = fib(i64(15)) + assert ans == i64(610) + +def main(): + # test of issue-529 + ans: i64 + ans = fib(i64(10)) + assert ans == i64(55) + + +main0() +main() diff --git a/mayhem/c-backend/testsuite/variable_decl_03.py b/mayhem/c-backend/testsuite/variable_decl_03.py new file mode 100755 index 0000000000..f554b9fc5d --- /dev/null +++ b/mayhem/c-backend/testsuite/variable_decl_03.py @@ -0,0 +1,11 @@ +from lpython import f64 + +def f() -> f64: + return abs(-5.5) + +def main(): + t1: f64 = f() * 1e6 + print(t1) + assert abs(t1 - 5.5 * 1e6) <= 1e-6 + +main() diff --git a/mayhem/config.json b/mayhem/config.json new file mode 100644 index 0000000000..e95a6f5bd9 --- /dev/null +++ b/mayhem/config.json @@ -0,0 +1,4 @@ +{ + "spec_version": 2, + "upstream_default_branch": "main" +} diff --git a/mayhem/cpp-python/testsuite/assert1.py b/mayhem/cpp-python/testsuite/assert1.py new file mode 100755 index 0000000000..7f6e98b6d5 --- /dev/null +++ b/mayhem/cpp-python/testsuite/assert1.py @@ -0,0 +1,5 @@ +def test_assert(): + a: i32 + a = 5 + assert a == 5, 'a is not 5' + assert a != 10 diff --git a/mayhem/cpp-python/testsuite/expr12.py b/mayhem/cpp-python/testsuite/expr12.py new file mode 100755 index 0000000000..73d7409390 --- /dev/null +++ b/mayhem/cpp-python/testsuite/expr12.py @@ -0,0 +1,13 @@ +def test(a: i32, b: i32) -> i32: + return a**b + +def check() -> i32: + a: i32 + a = test(2, 2) + return a + +def main0(): + x: i32 + x = check() + +main0() diff --git a/mayhem/cpp-python/testsuite/expr15.py b/mayhem/cpp-python/testsuite/expr15.py new file mode 100755 index 0000000000..09d03f2f07 --- /dev/null +++ b/mayhem/cpp-python/testsuite/expr15.py @@ -0,0 +1,25 @@ +from lpython import f64, c64, i32, c32 + +def test1() -> f64: + x: f64 + x = 1.0 + return x + + +def test2() -> c64: + x: c64 + x = complex(2, 2) + return x + + +def test3() -> i32: + x: c64 + x = 4j + y: c32 + y = c32(4j) + return 0 + + +print(test1()) +print(test2()) +print(test3()) diff --git a/mayhem/cpp-python/testsuite/expr2.py b/mayhem/cpp-python/testsuite/expr2.py new file mode 100755 index 0000000000..23deba2ee9 --- /dev/null +++ b/mayhem/cpp-python/testsuite/expr2.py @@ -0,0 +1,11 @@ +def test_boolOp(): + a: bool + b: bool + a = False + b = True + a = a and b + b = a or True + a = a or b + a = a and b == b + a = a and b != b + a = b or b diff --git a/mayhem/cpp-python/testsuite/expr6.py b/mayhem/cpp-python/testsuite/expr6.py new file mode 100755 index 0000000000..c9ecaefbe5 --- /dev/null +++ b/mayhem/cpp-python/testsuite/expr6.py @@ -0,0 +1,7 @@ +def test_ifexp(): + a: i32 + b: i32 + c: bool + a = 2 + b = 6 if a == 2 else 8 + c = True if b > 5 else False diff --git a/mayhem/cpp-python/testsuite/expr7.py b/mayhem/cpp-python/testsuite/expr7.py new file mode 100755 index 0000000000..e46c98a225 --- /dev/null +++ b/mayhem/cpp-python/testsuite/expr7.py @@ -0,0 +1,16 @@ +from lpython import i32 +def test_pow(): + a: i32 + a = i32(pow(2, 2)) + +def test_pow_1(a: i32, b: i32) -> i32: + res: i32 + res = i32(pow(a, b)) + return res + +def main0(): + test_pow() + c: i32 + c = test_pow_1(1, 2) + +main0() diff --git a/mayhem/cpp-python/testsuite/expr8.py b/mayhem/cpp-python/testsuite/expr8.py new file mode 100755 index 0000000000..1ba74bc3c9 --- /dev/null +++ b/mayhem/cpp-python/testsuite/expr8.py @@ -0,0 +1,23 @@ +from lpython import i32, f32 + +def test_binop(): + x: i32 + x2: f32 + x = 2**3 + x2 = f32(2.0**3.5) + x = 54 - 100 + x2 = f32((3.454 - 765.43) + 534.6) + x2 = f32(5346.565 * 3.45) + x2 = f32(5346.565 ** 3.45) + + # binop on logical type + x = True + True + x = True - False + x = True * False + x = True ** False + b1: bool + b2: bool + b1 = True + b2 = False + x = i32(b1//b1) + x = b1**b2 diff --git a/mayhem/cpp-python/testsuite/expr_11.py b/mayhem/cpp-python/testsuite/expr_11.py new file mode 100755 index 0000000000..5f97eb9fc6 --- /dev/null +++ b/mayhem/cpp-python/testsuite/expr_11.py @@ -0,0 +1,11 @@ +from lpython import i32 + +def f(): + i: i32 + i = 3 + b: bool + b = bool(i) + assert b + print(b) + +f() diff --git a/mayhem/cpp-python/testsuite/loop3.py b/mayhem/cpp-python/testsuite/loop3.py new file mode 100755 index 0000000000..56f9a8a0b0 --- /dev/null +++ b/mayhem/cpp-python/testsuite/loop3.py @@ -0,0 +1,5 @@ +def test_pass(): + a: i32 + a = 1 + while a > 0: + pass diff --git a/mayhem/cpp-python/testsuite/loop4.py b/mayhem/cpp-python/testsuite/loop4.py new file mode 100755 index 0000000000..e1ecf21d6d --- /dev/null +++ b/mayhem/cpp-python/testsuite/loop4.py @@ -0,0 +1,11 @@ +def test_for(): + i: i32 + for i in range(0, 10): + if i == 0: + continue + if i > 5: + break + if i == 3: + quit() + +test_for() diff --git a/mayhem/cpp-python/testsuite/test_builtin_pow.py b/mayhem/cpp-python/testsuite/test_builtin_pow.py new file mode 100755 index 0000000000..3402c0d9e6 --- /dev/null +++ b/mayhem/cpp-python/testsuite/test_builtin_pow.py @@ -0,0 +1,89 @@ +from lpython import i32, i64, f32, f64, c32 + +def test_pow(): + # TODO: the commented tests should also work + a: i32 + b: i32 + eps: f64 + eps = 1e-12 + a = 2 + b = 5 + assert i32(pow(a, b)) == 32 + a = 6 + b = 3 + assert i32(pow(a, b)) == 216 + a = 2 + b = 0 + assert i32(pow(a, b)) == 1 + a = 2 + b = -1 + # assert abs(pow(a, b) - 0.5) < eps + a = 6 + b = -4 + # assert abs(pow(a, b) - 0.0007716049382716049) < eps + + i1: i64 + i2: i64 + i1 = i64(2) + i2 = i64(5) + assert i64(pow(i1, i2)) == i64(32) + i1 = i64(6) + i2 = -i64(3) + # assert abs(pow(i1, i2) - 0.004629629629629629) < eps + + f1: f32 + f2: f32 + p: f32 + f1 = f32(525346/66456) + f2 = f32(3.0) + p = pow(f1, f2) + + f1 = pow(a, f2) # (i32, f32) + f1 = pow(f2, a) # (f32, i32) + + b1: bool = True + b2: bool = False + assert pow(b1, b2) == 1 + assert pow(b2, b1) == 0 + assert pow(b1, b2) == 1 + assert pow(False, False) == 1 + + a1: f64 = 4.5 + a2: f64 = 2.3 + assert abs(pow(a1, a2) - 31.7971929089206) < eps + assert abs(pow(a2, a1) - 42.43998894277659) < eps + + x: i32 = 3 + y: f64 = 2.3 + assert abs(pow(x, y) - 12.513502532843182) < eps + assert abs(pow(y, x) - 12.166999999999998) < eps + assert abs(pow(x, 5.5) - 420.8883462392372) < eps + + assert abs(pow(i64(2), -i64(1)) - 0.5) < eps + assert abs(pow(i64(6), -i64(4)) - 0.0007716049382716049) < eps + assert abs(pow(-i64(3), -i64(5)) + 0.00411522633744856) < eps + assert abs(pow(i64(6), -i64(4)) - 0.0007716049382716049) < eps + assert abs(pow(4.5, 2.3) - 31.7971929089206) < eps + assert abs(pow(2.3, 0.0) - 1.0) < eps + assert abs(pow(2.3, -1.5) - 0.2866871623459944) < eps + assert abs(pow(2, 3.4) - 10.556063286183154) < eps + assert abs(pow(2, -3.4) - 0.09473228540689989) < eps + assert abs(pow(3.4, 9) - 60716.99276646398) < eps + assert abs(pow(0.0, 53) - 0.0) < eps + assert i32(pow(4, 2)) == 16 + assert abs(pow(-4235.0, 52) - 3.948003805985264e+188) < eps + + i: i64 + i = i64(7) + j: i64 + j = i64(2) + k: i64 + k = i64(5) + assert pow(i, j, k) == i64(4) + # assert pow(102, 3, 121) == 38 + + c1: c32 + c1 = c32(complex(4, 5)) + c1 = pow(c1, 4) + +test_pow() diff --git a/mayhem/cpp-python/testsuite/test_list_repeat2.py b/mayhem/cpp-python/testsuite/test_list_repeat2.py new file mode 100755 index 0000000000..e5fb297249 --- /dev/null +++ b/mayhem/cpp-python/testsuite/test_list_repeat2.py @@ -0,0 +1,24 @@ +from lpython import i32, f32 + +def add_list(x: list[f32]) -> f32: + sum: f32 = f32(0.0) + i: i32 + + for i in range(len(x)): + sum = sum + f32(x[i]) + return sum + +def create_list(n: i32) -> list[f32]: + x: list[f32] + i: i32 + + x = [f32(0.0)] * n + for i in range(n): + x[i] = f32(i) + return x + +def main0(): + x: list[f32] = create_list(i32(10)) + print(add_list(x)) + +main0() diff --git a/mayhem/harnesses/fuzz_asr.cpp b/mayhem/harnesses/fuzz_asr.cpp new file mode 100644 index 0000000000..6f748c6b1d --- /dev/null +++ b/mayhem/harnesses/fuzz_asr.cpp @@ -0,0 +1,14 @@ +// lpython --show-asr: parse -> python_ast_to_asr (semantics) -> default ASR passes -> ASR pickle. +#include "lpython_fuzz_common.h" + +void lpfuzz_run(lpfuzz::Ctx &ctx) { + LCompilers::ASR::TranslationUnit_t *asr = ctx.source_to_asr(); + if (!asr) return; + LCompilers::PassManager pass_manager; + ctx.co.po.always_run = true; + ctx.co.po.run_fun = "f"; + pass_manager.apply_passes(ctx.al, asr, ctx.co.po, ctx.diagnostics); + ctx.render(); + std::string s = LCompilers::pickle(*asr, false, false, false); + (void)s; +} diff --git a/mayhem/harnesses/fuzz_c.cpp b/mayhem/harnesses/fuzz_c.cpp new file mode 100644 index 0000000000..f797f1dcf5 --- /dev/null +++ b/mayhem/harnesses/fuzz_c.cpp @@ -0,0 +1,18 @@ +// lpython --show-c: parse -> ASR -> C-backend pass pipeline -> asr_to_c. +#include "lpython_fuzz_common.h" +#include + +void lpfuzz_run(lpfuzz::Ctx &ctx) { + LCompilers::ASR::TranslationUnit_t *asr = ctx.source_to_asr(); + if (!asr) return; + LCompilers::PassManager pass_manager; + pass_manager.use_default_passes(true); + ctx.co.po.always_run = true; + ctx.co.po.run_fun = "f"; + ctx.co.po.c_skip_bindpy_pass = true; + pass_manager.apply_passes(ctx.al, asr, ctx.co.po, ctx.diagnostics); + ctx.diagnostics.diagnostics.clear(); + auto res = LCompilers::asr_to_c(ctx.al, *asr, ctx.diagnostics, ctx.co, 0); + ctx.render(); + (void)res; +} diff --git a/mayhem/harnesses/fuzz_cpp_python.cpp b/mayhem/harnesses/fuzz_cpp_python.cpp new file mode 100644 index 0000000000..7cf2ab0e8b --- /dev/null +++ b/mayhem/harnesses/fuzz_cpp_python.cpp @@ -0,0 +1,29 @@ +// lpython --show-cpp / --show-python: parse -> ASR -> asr_to_cpp or asr_to_python. The first input +// byte picks the backend (odd = python); the rest is the Python source. +#include "lpython_fuzz_common.h" +#include +#include + +void lpfuzz_run(lpfuzz::Ctx &ctx) { + if (ctx.input.empty()) return; + bool python = ctx.input[0] & 1; + std::string src = ctx.input.substr(1); + ctx.input = src; + ctx.lm = LCompilers::LocationManager(); + LCompilers::LocationManager::FileLocations fl; + fl.in_filename = "input.py"; + ctx.lm.files.push_back(fl); + ctx.lm.init_simple(ctx.input); + ctx.lm.file_ends.push_back(ctx.input.size()); + + LCompilers::ASR::TranslationUnit_t *asr = ctx.source_to_asr(); + if (!asr) return; + if (python) { + auto r = LCompilers::asr_to_python(ctx.al, *asr, ctx.diagnostics, ctx.co, false, 0); + (void)r; + } else { + auto r = LCompilers::asr_to_cpp(ctx.al, *asr, ctx.diagnostics, ctx.co, 0); + (void)r; + } + ctx.render(); +} diff --git a/mayhem/harnesses/fuzz_parse.cpp b/mayhem/harnesses/fuzz_parse.cpp new file mode 100644 index 0000000000..ed3e10e357 --- /dev/null +++ b/mayhem/harnesses/fuzz_parse.cpp @@ -0,0 +1,9 @@ +// lpython --show-ast: tokenizer (re2c) + parser (bison) + AST pickling + diagnostics rendering. +#include "lpython_fuzz_common.h" + +void lpfuzz_run(lpfuzz::Ctx &ctx) { + LCompilers::LPython::AST::ast_t *ast = ctx.parse(); + if (!ast) return; + std::string s = LCompilers::LPython::pickle_python(*ast, false, false); + (void)s; +} diff --git a/mayhem/harnesses/fuzz_wasm_x64.cpp b/mayhem/harnesses/fuzz_wasm_x64.cpp new file mode 100644 index 0000000000..3179479f99 --- /dev/null +++ b/mayhem/harnesses/fuzz_wasm_x64.cpp @@ -0,0 +1,44 @@ +// lpython --backend wasm_x64 / wasm_x86: parse -> ASR -> wasm bytes -> wasm_to_x64 or wasm_to_x86 +// (x86 assembler + ELF writer). The first input byte picks the backend (odd = x86); the rest is +// the Python source. The ELF goes to a private temp file that is removed; it is never executed. +#include "lpython_fuzz_common.h" +#include +#include +#include +#include + +void lpfuzz_run(lpfuzz::Ctx &ctx) { + if (ctx.input.empty()) return; + bool x86 = ctx.input[0] & 1; + // Re-seat the context on the source without the selector byte. + std::string src = ctx.input.substr(1); + ctx.input = src; + ctx.lm = LCompilers::LocationManager(); + LCompilers::LocationManager::FileLocations fl; + fl.in_filename = "input.py"; + ctx.lm.files.push_back(fl); + ctx.lm.init_simple(ctx.input); + ctx.lm.file_ends.push_back(ctx.input.size()); + + LCompilers::ASR::TranslationUnit_t *asr = ctx.source_to_asr(); + if (!asr) return; + LCompilers::Result> wasm = + LCompilers::asr_to_wasm_bytes_stream(*asr, ctx.al, ctx.diagnostics, ctx.co); + ctx.render(); + if (!wasm.ok) return; + + const char *tmpdir = getenv("TMPDIR"); + std::string tmpl = std::string(tmpdir && *tmpdir ? tmpdir : "/tmp") + "/lpfuzz_XXXXXX"; + int fd = mkstemp(&tmpl[0]); + if (fd < 0) return; + close(fd); + if (x86) { + auto r = LCompilers::wasm_to_x86(wasm.result, ctx.al, tmpl, false, ctx.diagnostics); + (void)r; + } else { + auto r = LCompilers::wasm_to_x64(wasm.result, ctx.al, tmpl, false, ctx.diagnostics); + (void)r; + } + unlink(tmpl.c_str()); + ctx.render(); +} diff --git a/mayhem/harnesses/fuzz_wat.cpp b/mayhem/harnesses/fuzz_wat.cpp new file mode 100644 index 0000000000..bd8b9fbbd6 --- /dev/null +++ b/mayhem/harnesses/fuzz_wat.cpp @@ -0,0 +1,16 @@ +// lpython --show-wat: parse -> ASR -> asr_to_wasm_bytes_stream -> wasm_to_wat (wasm decoder). +#include "lpython_fuzz_common.h" +#include +#include + +void lpfuzz_run(lpfuzz::Ctx &ctx) { + LCompilers::ASR::TranslationUnit_t *asr = ctx.source_to_asr(); + if (!asr) return; + LCompilers::Result> wasm = + LCompilers::asr_to_wasm_bytes_stream(*asr, ctx.al, ctx.diagnostics, ctx.co); + ctx.render(); + if (!wasm.ok) return; + LCompilers::Result wat = LCompilers::wasm_to_wat(wasm.result, ctx.al, ctx.diagnostics); + ctx.render(); + (void)wat; +} diff --git a/mayhem/harnesses/lpython_fuzz_common.h b/mayhem/harnesses/lpython_fuzz_common.h new file mode 100644 index 0000000000..0b82a4ff02 --- /dev/null +++ b/mayhem/harnesses/lpython_fuzz_common.h @@ -0,0 +1,120 @@ +// Shared driver for the lpython libFuzzer harnesses. +// +// Each harness mirrors one `lpython --show-*` / `--backend` path from src/bin/lpython.cpp +// (emit_ast, emit_asr, emit_c, emit_wat, compile_to_binary_*), but takes the Python source +// from the fuzzer buffer instead of a file. Errors are reported by the compiler as diagnostics +// or LCompilersException-family exceptions, which the CLI's main() catches and reports; we do +// the same, so only real memory-safety / UB defects (ASan/UBSan) or aborts surface. +// +// State kept across inputs: only the runtime-library directory (read-only, set once from the +// binary's own location). Every input gets a fresh Allocator, Diagnostics, LocationManager and +// CompilerOptions. +#pragma once + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace lpfuzz { + +// Runtime library (.py modules like lpython_builtin.py) ships in the source tree at +// /src/runtime; the fuzz binaries live in the repo root, so locate it relative to the +// executable (no absolute path baked in). +inline void init_runtime_dir() { + if (std::getenv("LFORTRAN_RUNTIME_LIBRARY_DIR")) return; + char buf[PATH_MAX]; + ssize_t n = readlink("/proc/self/exe", buf, sizeof(buf) - 1); + if (n <= 0) return; + buf[n] = '\0'; + std::string exe(buf); + std::string dir = exe.substr(0, exe.rfind('/')); + std::string rt = dir + "/src/runtime"; + setenv("LFORTRAN_RUNTIME_LIBRARY_DIR", rt.c_str(), 1); +} + +struct Ctx { + Allocator al{4 * 1024}; + LCompilers::diag::Diagnostics diagnostics; + LCompilers::LocationManager lm; + LCompilers::CompilerOptions co; + std::string input; + + Ctx(const uint8_t *data, size_t size) : input((const char *)data, size) { + co.use_colors = false; + co.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); + LCompilers::LocationManager::FileLocations fl; + fl.in_filename = "input.py"; + lm.files.push_back(fl); + lm.init_simple(input); + lm.file_ends.push_back(input.size()); + } + + // Render (and discard) diagnostics: reaches LocationManager position mapping. + void render() { + std::string s = diagnostics.render(lm, co); + (void)s; + diagnostics.diagnostics.clear(); + } + + // Source -> AST (lpython --show-ast). nullptr on a parse error. + LCompilers::LPython::AST::ast_t *parse() { + LCompilers::Result r = + LCompilers::LPython::parse(al, input, 0, diagnostics); + render(); + if (!r.ok) return nullptr; + return (LCompilers::LPython::AST::ast_t *)r.result; + } + + // AST -> ASR (lpython --show-asr, without passes). nullptr on a semantic error. + LCompilers::ASR::TranslationUnit_t *to_asr(LCompilers::LPython::AST::ast_t *ast) { + LCompilers::Result r = + LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, co, + true, "__main__", "input.py"); + render(); + if (!r.ok) return nullptr; + return r.result; + } + + LCompilers::ASR::TranslationUnit_t *source_to_asr() { + LCompilers::LPython::AST::ast_t *ast = parse(); + if (!ast) return nullptr; + return to_asr(ast); + } +}; + +} // namespace lpfuzz + +// Each harness defines this; it runs one compiler path on the context. +void lpfuzz_run(lpfuzz::Ctx &ctx); + +extern "C" int LLVMFuzzerInitialize(int *, char ***) { + lpfuzz::init_runtime_dir(); + return 0; +} + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + try { + lpfuzz::Ctx ctx(data, size); + lpfuzz_run(ctx); + } catch (const LCompilers::LCompilersException &) { + // Internal compiler error reported as an exception: main() prints it and exits 1. + } catch (const std::runtime_error &) { + } catch (const std::exception &) { + } catch (...) { + // main() also swallows unknown exceptions ("Unknown error"). + } + return 0; +} diff --git a/mayhem/lsan_off.cc b/mayhem/lsan_off.cc new file mode 100644 index 0000000000..e919968c20 --- /dev/null +++ b/mayhem/lsan_off.cc @@ -0,0 +1 @@ +extern "C" int __lsan_is_turned_off() { return 1; } diff --git a/mayhem/parse/testsuite/assert1.py b/mayhem/parse/testsuite/assert1.py new file mode 100755 index 0000000000..bed46b99d9 --- /dev/null +++ b/mayhem/parse/testsuite/assert1.py @@ -0,0 +1,5 @@ +def test_assert(): + a: i32 + a = 5 + assert a == 5, 'a is not 5' + assert a != 10 diff --git a/mayhem/parse/testsuite/complex1.py b/mayhem/parse/testsuite/complex1.py new file mode 100755 index 0000000000..5b16a6c7eb --- /dev/null +++ b/mayhem/parse/testsuite/complex1.py @@ -0,0 +1,38 @@ +from lpython import c32, c64 + +def test_complex(): + c: c32 + c1: c32 + c2: c32 + c3: c64 + b: bool + + # constant real or int as args + c = c32(complex()) + c = c32(complex(3.4)) + c = c32(complex(5., 4.3)) + c = c32(complex(1)) + c1 = c32(complex(3, 4)) + c2 = c32(complex(2, 4.5)) + c3 = complex(3., 4.) + b = c1 != c2 + b = c64(c1) == c3 + + # binary ops + c = c1 + c2 + c = c2 - c1 + c = c1 * c2 + c = c32(complex(1, 2) ** complex(3.34534, 4.8678678)) + c = c32(complex(1, 2) * complex(3, 4)) + c = c32(complex(4, 5) - complex(3, 4)) + + +def test(): + x: c64 + y: c64 + z: c32 + x = c64(2) + 3j + y = c64(5) + 5j + z = c32(x + y) + z = c32(x - y) + z = c32(c64(2) * x) diff --git a/mayhem/parse/testsuite/constants1.py b/mayhem/parse/testsuite/constants1.py new file mode 100755 index 0000000000..3fa8b6d34f --- /dev/null +++ b/mayhem/parse/testsuite/constants1.py @@ -0,0 +1,118 @@ +from lpython import f32, i32, i64, f64 + +def test_boz(): + b: str + b = bin(5) + b = bin(64) + b = bin(-534) + b = oct(8) + b = oct(56) + b = oct(-534) + b = hex(42) + b = hex(12648430) + b = hex(-534) + + +def test_ord_chr(): + s: str + a: i32 + a = ord('5') + s = chr(43) + + +def test_abs(): + a: i32 + a = abs(5) + a = abs(-500) + a = abs(i32(False)) + a = abs(i32(True)) + b: f32 + b = f32(abs(3.45)) + b = f32(abs(-5346.34)) + b = f32(abs(complex(3.45, 5.6))) + + +def test_len(): + a: i32 + a = len('') + a = len('test') + a = len("this is a test") + a = len((1, 2, 3)) + a = len((("c", "b", 3.4), ("c", 3, 5.6))) + a = len([1, 2, 3]) + a = len([[-4, -5, -6], [-1, -2, -3]]) + a = len({1, 2, 3}) + a = len({1: "c", 2: "b", 3: "c"}) + l: list[i32] + l = [1, 2, 3, 4] + a = len(l) + l.append(5) + a = len(l) + + +def test_bool(): + a: bool + a = bool(0) + a = bool(-1) + a = bool('') + a = bool(complex(0, 0)) + assert a == False + a = bool('t') + a = bool(2.3) + assert a == True + + +def test_str(): + s: str + s = str() + s = str(5) + s = str(-4) + s = str(5.6) + s = str(True) + s = str(False) + s = str("5346") + + +def test_callable(): + a: bool + b: i32 + b = 2 + a = callable(test_len) + assert a == True + a = callable(b) + assert a == False + a = callable('c') + assert a == False + + +def test_int(): + a: i64 + a = int() + a = int(4.56) + a = int(5) + a = int(-5.00001) + a = int(True) + a = int(False) + a = int("5346") + + +def test_float(): + a: f64 + a = float() + a = float(4.56) + a = float(5) + a = float(-1) + a = float(True) + a = float(False) + # commented tests should work + # a = float("5346") + # a = float("423.534") + + +def test_divmod(): + a: tuple[i32, i32] + a = divmod(9, 3) + a = divmod(9, -3) + a = divmod(3, 3) + a = divmod(4, 5) + a = divmod(0, 5) diff --git a/mayhem/parse/testsuite/dictionary1.py b/mayhem/parse/testsuite/dictionary1.py new file mode 100755 index 0000000000..b50e326d5a --- /dev/null +++ b/mayhem/parse/testsuite/dictionary1.py @@ -0,0 +1,41 @@ +def test_Dict(): + x: dict[i32, i32] + x = {1: 2, 3: 4} + # x = {1: "2", "3": 4} -> semantic error + + y: dict[str, i32] + y = {"a": -1, "b": -2} + + z: i32 + z = y["a"] + z = y["b"] + z = x[1] + + +def test_dict_insert(): + y: dict[str, i32] + y = {"a": -1, "b": -2} + y["c"] = -3 + + +def test_dict_get(): + y: dict[str, i32] + y = {"a": -1, "b": -2} + x: i32 + x = y.get("a") + x = y.get("a", 0) + + +def test_dict_pop(): + y: dict[str, i32] + y = {"a": 1, "b": 2} + x: i32 + x = y.pop("a") + + +def f(x: InOut[dict[i32, i32]]): + x[2] = 4 + +def test_issue_204(): + x: dict[i32, i32] + f(x) diff --git a/mayhem/parse/testsuite/ellipsis1.py b/mayhem/parse/testsuite/ellipsis1.py new file mode 100755 index 0000000000..23f4436da8 --- /dev/null +++ b/mayhem/parse/testsuite/ellipsis1.py @@ -0,0 +1,37 @@ +import numpy as np +from typing import Callable + +array = np.random.rand(2, 2, 2, 2) +print(array[..., 0]) +print(array[Ellipsis, 0]) + +def inject(get_next_item: Callable[..., str]) -> None: + ... + +def foo(x: ...) -> None: + ... + +class flow: + def __understand__(self, name: str, value: ...) -> None: ... + +def foo(x = ...): + return x + +def test(): + ... + +class Todo: + ... + +x = [1, [2, [...], 3]] +l = [..., 1, 2, 3] + +if x is ...: + pass + +def partial(func: Callable[..., str], *args) -> Callable[..., str]: + pass + +class xyz: + abc: str = ... + def __init__(self, name: str=...) -> None: ... diff --git a/mayhem/parse/testsuite/expr1.py b/mayhem/parse/testsuite/expr1.py new file mode 100755 index 0000000000..4c889a09b8 --- /dev/null +++ b/mayhem/parse/testsuite/expr1.py @@ -0,0 +1,15 @@ +def test_namedexpr(): + a: i32 + x: i32 + y: i32 + + # check 1 + x = (y := 0) + + # check 2 + if a := ord('3'): + x = 1 + + # check 3 + while a := 1: + y = 1 diff --git a/mayhem/parse/testsuite/expr10.py b/mayhem/parse/testsuite/expr10.py new file mode 100755 index 0000000000..4838f3dc19 --- /dev/null +++ b/mayhem/parse/testsuite/expr10.py @@ -0,0 +1,31 @@ +from lpython import i32, f32, c32 + +def test_UnaryOp(): + a: i32 + a = +4 + a = -500 + a = ~5 + b: bool + b = not 5 + b = not -1 + b = not 0 + + f: f32 + f = +f32(1.0) + f = -f32(183745.534) + + b1: bool + b2: bool + b3: bool + b1 = True + b2 = not False + b3 = not b2 + a = +True + a = -False + a = ~True + + c: c32 + c = +c32(complex(1, 2)) + c = -c32(complex(3, 65.0)) + b1 = not complex(3, 4) + b2 = not complex(0, 0) diff --git a/mayhem/parse/testsuite/expr12.py b/mayhem/parse/testsuite/expr12.py new file mode 100755 index 0000000000..211b3e1cb4 --- /dev/null +++ b/mayhem/parse/testsuite/expr12.py @@ -0,0 +1,13 @@ +def test(a: i32, b: i32) -> i32: + return a**b + +def check() -> i32: + a: i32 + a = test(2, 2) + return a + +def main0(): + x: i32 + x = check() + +main0() diff --git a/mayhem/parse/testsuite/expr13.py b/mayhem/parse/testsuite/expr13.py new file mode 100755 index 0000000000..48a3984abf --- /dev/null +++ b/mayhem/parse/testsuite/expr13.py @@ -0,0 +1,23 @@ +def test_Compare(): + a: bool + a = 5 > 4 + a = 5 <= 4 + a = 5 < 4 + a = 5.6 >= 5.59999 + a = 3.3 == 3.3 + a = 3.3 != 3.4 + a = complex(3, 4) == complex(3., 4.) + + # string comparison + a = "abc" > "abd" + a = "" < "s" + a = "-abs" >= "abs" + a = "abcd" <= "abcde" + a = "abc" == "abc" + a = "abc" != "abd" + a = "" == "+" + + a = True > False + a = True == True + a = False != True + a = False >= True diff --git a/mayhem/parse/testsuite/expr2.py b/mayhem/parse/testsuite/expr2.py new file mode 100755 index 0000000000..b26a96cc6e --- /dev/null +++ b/mayhem/parse/testsuite/expr2.py @@ -0,0 +1,11 @@ +def test_boolOp(): + a: bool + b: bool + a = False + b = True + a = a and b + b = a or True + a = a or b + a = a and b == b + a = a and b != b + a = b or b diff --git a/mayhem/parse/testsuite/expr4.py b/mayhem/parse/testsuite/expr4.py new file mode 100755 index 0000000000..030b93569e --- /dev/null +++ b/mayhem/parse/testsuite/expr4.py @@ -0,0 +1,6 @@ +def test_del(): + a: i32 + b: i32 + a = 4 + b = 20 + del a, b diff --git a/mayhem/parse/testsuite/expr6.py b/mayhem/parse/testsuite/expr6.py new file mode 100755 index 0000000000..a7534d4a33 --- /dev/null +++ b/mayhem/parse/testsuite/expr6.py @@ -0,0 +1,7 @@ +def test_ifexp(): + a: i32 + b: i32 + c: bool + a = 2 + b = 6 if a == 2 else 8 + c = True if b > 5 else False diff --git a/mayhem/parse/testsuite/expr7.py b/mayhem/parse/testsuite/expr7.py new file mode 100755 index 0000000000..58125ba341 --- /dev/null +++ b/mayhem/parse/testsuite/expr7.py @@ -0,0 +1,16 @@ +from lpython import i32 +def test_pow(): + a: i32 + a = i32(pow(2, 2)) + +def test_pow_1(a: i32, b: i32) -> i32: + res: i32 + res = i32(pow(a, b)) + return res + +def main0(): + test_pow() + c: i32 + c = test_pow_1(1, 2) + +main0() diff --git a/mayhem/parse/testsuite/expr8.py b/mayhem/parse/testsuite/expr8.py new file mode 100755 index 0000000000..ac3bcc7db4 --- /dev/null +++ b/mayhem/parse/testsuite/expr8.py @@ -0,0 +1,23 @@ +from lpython import i32, f32 + +def test_binop(): + x: i32 + x2: f32 + x = 2**3 + x2 = f32(2.0**3.5) + x = 54 - 100 + x2 = f32((3.454 - 765.43) + 534.6) + x2 = f32(5346.565 * 3.45) + x2 = f32(5346.565 ** 3.45) + + # binop on logical type + x = True + True + x = True - False + x = True * False + x = True ** False + b1: bool + b2: bool + b1 = True + b2 = False + x = i32(b1//b1) + x = b1**b2 diff --git a/mayhem/parse/testsuite/expr_01.py b/mayhem/parse/testsuite/expr_01.py new file mode 100755 index 0000000000..954183f577 --- /dev/null +++ b/mayhem/parse/testsuite/expr_01.py @@ -0,0 +1,9 @@ +def main0(): + x: i32 + x2: i64 + y: f32 + y2: f64 + x = (2+3)*5 + print(x) + +main0() diff --git a/mayhem/parse/testsuite/global1.py b/mayhem/parse/testsuite/global1.py new file mode 100755 index 0000000000..12acdba6ab --- /dev/null +++ b/mayhem/parse/testsuite/global1.py @@ -0,0 +1,26 @@ +x = "global " + +def outer(): + x = "local" + + def inner(): + nonlocal x + x = "nonlocal" + assert x == "nonlocal" + inner() + + assert x == "nonlocal" + +def test_1(): + global x + y = "local" + x = x * 2 + assert x == "global global " + assert y == "local" + + +def check(): + test_1() + outer() + +check() diff --git a/mayhem/parse/testsuite/global_scope1.py b/mayhem/parse/testsuite/global_scope1.py new file mode 100755 index 0000000000..594b43caa7 --- /dev/null +++ b/mayhem/parse/testsuite/global_scope1.py @@ -0,0 +1,2 @@ +i: i32 +i = 5 diff --git a/mayhem/parse/testsuite/list1.py b/mayhem/parse/testsuite/list1.py new file mode 100755 index 0000000000..62a71cd9ae --- /dev/null +++ b/mayhem/parse/testsuite/list1.py @@ -0,0 +1,33 @@ +def test_List(): + a: list[i32] + a = [1, 2, 3] + a = [-3, -2, -1] + # a = [-2, -1, 0.45] -> semantic error + + b: list[str] + b = ["a", "b", "c"] + + c: list[list[i32]] + c = [[1, 2, 3], [4, 5, 6]] + + d: i32 + d = a[2] + + # ragged list + e: list[list[str]] + e = [['a', 'b', 'c'], ['d', 'e']] + + a.append(10) + a.remove(1) + a.insert(2, 13) + a = a[0:2] + d = a.pop() + d = a.pop(2) + a += [4, 5] + a = [6, 7] + a + + a11: list[i32] + b11: list[i32] + a11 = [1, 2] + b11 = [3, 4] + assert a11 == b11 diff --git a/mayhem/parse/testsuite/loop3.py b/mayhem/parse/testsuite/loop3.py new file mode 100755 index 0000000000..fd73a974f1 --- /dev/null +++ b/mayhem/parse/testsuite/loop3.py @@ -0,0 +1,5 @@ +def test_pass(): + a: i32 + a = 1 + while a > 0: + pass diff --git a/mayhem/parse/testsuite/set1.py b/mayhem/parse/testsuite/set1.py new file mode 100755 index 0000000000..e8d5d7bb3c --- /dev/null +++ b/mayhem/parse/testsuite/set1.py @@ -0,0 +1,14 @@ +def test_Set(): + a: set[i32] + a = {1, 2, 3} + a = {2, 3, 4, 5, 5} + + a.add(9) + a.remove(4) + + b: set[str] + b = {"a", "b", "c"} + # a = {-1, -2, "c"} -> semantic error for now + + s: str + s = b.pop() diff --git a/mayhem/parse/testsuite/tuple1.py b/mayhem/parse/testsuite/tuple1.py new file mode 100755 index 0000000000..cd885e82d2 --- /dev/null +++ b/mayhem/parse/testsuite/tuple1.py @@ -0,0 +1,35 @@ +from lpython import i32, f32 + +def test_Tuple(): + a1: tuple[i32, i32, i32] + a1 = (1, 2, 3) + a1 = (-3, -2, -1) + + a2: tuple[str, str, str] + a2 = ("a", "b", "c") + + a3: tuple[i32, i32, f32, str] + float_mem: f32 + float_mem = f32(0.45) + a3 = (-2, -1, float_mem, "d") + + a4: tuple[tuple[i32, i32, i32], tuple[i32, i32, i32]] + a4 = ((1, 2, 3), (4, 5, 6)) + + a5: tuple[tuple[str, str, f32], tuple[str, i32, f32]] + float_mem1: f32 + float_mem2: f32 + float_mem1 = f32(3.4) + float_mem2 = f32(5.6) + a5 = (("a", "b", float_mem1), ("c", 3, float_mem2)) + + b0: i32 + b1: i32 + b0 = a1[0] + b0, b1 = a1[2], a1[1] + + a11: tuple[i32, i32] + b11: tuple[i32, i32] + a11 = (1, 2) + b11 = (1, 2) + assert a11 == b11 diff --git a/mayhem/reference_tests.py b/mayhem/reference_tests.py new file mode 100755 index 0000000000..0caae26c72 --- /dev/null +++ b/mayhem/reference_tests.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +"""Golden-output oracle: replay upstream's tests/reference/*.json (the same records run_tests.py +checks) for the non-LLVM frontends/backends and compare stdout + return code with the committed +reference output. Prints `PASSED ` / `FAILED ` and the names of failing tests.""" +import glob, json, os, subprocess, sys + +KINDS = ("tokens", "ast", "ast_new", "asr", "c", "cpp", "python", "wat") +lpython = sys.argv[1] +passed = failed = 0 +bad = [] +for ref in sorted(glob.glob("tests/reference/*.json")): + with open(ref) as f: + j = json.load(f) + kind = os.path.basename(ref).split("-", 1)[0] + cmd = j.get("cmd", "") + if kind not in KINDS or not cmd.startswith("lpython ") or j.get("outfile"): + continue + if "{infile}" not in cmd or not j.get("stdout"): + continue + args = cmd.replace(" -o {outfile}", "").split()[1:] + args = [a.replace("{infile}", j["infile"]) for a in args] + args.insert(0, "--no-error-banner") + try: + p = subprocess.run([lpython] + args, capture_output=True, timeout=60) + out = p.stdout.decode("utf-8", "replace") + rc = p.returncode + except subprocess.TimeoutExpired: + out, rc = None, None + with open(os.path.join("tests/reference", j["stdout"]), encoding="utf-8", errors="replace") as f: + want = f.read() + if out is not None and rc == j.get("returncode", 0) and out.rstrip("\n") == want.rstrip("\n"): + passed += 1 + else: + failed += 1 + bad.append(j["basename"]) +for b in bad: + print("FAIL", b) +print("PASSED", passed) +print("FAILED", failed) diff --git a/mayhem/test.sh b/mayhem/test.sh new file mode 100755 index 0000000000..9b2258d136 --- /dev/null +++ b/mayhem/test.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# +# mayhem/test.sh — RUN this repo's OWN functional test suite (already built by mayhem/build.sh). +# exit 0 = pass. EDIT per repo. PATCH-grade oracle: after an agent patches the source, the grader +# rebuilds (build.sh) then runs this. DELETE this file if the repo has no meaningful tests. +# +# IMPORTANT: +# * Must assert BEHAVIOR/OUTPUT, not just exit status. The oracle has to check asserted values / +# golden-output diffs / known-answer results — so a PATCH that "fixes" a bug by making the program +# exit(0) (or any no-op) FAILS here. Running inputs and checking only "exit 0 / didn't crash" is +# NOT a functional test (it's trivially reward-hackable) — use the project's real assertion suite. +# * Do NOT build here — mayhem/build.sh already compiled the test suite (with the project's normal +# flags). This script only RUNS the pre-built tests and reports counts. If the test runner is +# missing, that's a build.sh bug — fail loudly rather than silently rebuilding. +# * REQUIRED OUTPUT — a CTRF (https://ctrf.io) summary so Mayhem/the PATCH grader reads the counts: +# - writes a CTRF JSON report to ${CTRF_REPORT:-$SRC/ctrf-report.json}, and +# - prints a one-line `CTRF {...}` marker to stdout (same JSON, compact). +# Only `results.summary` (with tests/passed/failed/pending/skipped/other) is required. +# Use the emit_ctrf helper below; it computes tests = passed+failed+skipped and sets the exit +# code (0 iff failed==0). Map your framework's output to passed/failed/skipped. +set -uo pipefail +[ -n "${SOURCE_DATE_EPOCH:-}" ] || unset SOURCE_DATE_EPOCH +: "${MAYHEM_JOBS:=$(nproc)}" # build parallelism; env-overridable, falls back to nproc (use -j"$MAYHEM_JOBS") +cd "$SRC" + +# emit_ctrf [skipped] [pending] [other] +# Writes a CTRF report (file + stdout `CTRF {...}` marker) and returns non-zero iff failed>0. +emit_ctrf() { + local tool="$1" passed="$2" failed="$3" skipped="${4:-0}" pending="${5:-0}" other="${6:-0}" + local tests=$(( passed + failed + skipped + pending + other )) + cat > "${CTRF_REPORT:-$SRC/ctrf-report.json}" <&2; emit_ctrf "lpython-reference" 0 1 0; exit 1; fi +out=$(python3 "$SRC/mayhem/reference_tests.py" "$LP") || true +echo "$out" | grep '^FAIL ' || true +passed=$(echo "$out" | awk '$1=="PASSED"{print $2}') +failed=$(echo "$out" | awk '$1=="FAILED"{print $2}') +: "${passed:=0}" ; : "${failed:=1}" +[ "$passed" -gt 0 ] || failed=$(( failed + 1 )) +emit_ctrf "lpython-reference" "$passed" "$failed" 0 diff --git a/mayhem/wasm-x64/testsuite/bool1.py b/mayhem/wasm-x64/testsuite/bool1.py new file mode 100755 index 0000000000000000000000000000000000000000..b614a624f945950382821ba86b44c2f4634cf511 GIT binary patch literal 129 zcmZQbNljBINi8mkPs-2F(a^NwQUHR2qRhM!jgX?!R81tlTVhTzR3OPp0i+%*nWSK= W08)yiG6|v$Bm>rjD#e9l9~S^I)+3$( literal 0 HcmV?d00001 diff --git a/mayhem/wasm-x64/testsuite/expr14.py b/mayhem/wasm-x64/testsuite/expr14.py new file mode 100755 index 0000000000000000000000000000000000000000..8b2af67e69859e879efbb939d983177865bce6be GIT binary patch literal 203 zcmZQbNljBINi8mkPs-2FNlna)PtMORNG!@M&d<}(wBk|#f+RyL1&}O+V}#5xM&=kQ z*eZk+m8L?~q411>Jh#N0VyGlYX_BFWokEfk)C!1zEl?O)#7IFAC}NDH#z?^qL&OFw F0sukhFZ%!h literal 0 HcmV?d00001 diff --git a/mayhem/wasm-x64/testsuite/expr2.py b/mayhem/wasm-x64/testsuite/expr2.py new file mode 100755 index 0000000000000000000000000000000000000000..daad0c745a280becbc941408e17615b965865977 GIT binary patch literal 177 zcmZQbNljBINi8mkPs-2F@h{NOwBk|#fz@GnR$N-W9D&nwo@wBk}wfXHYlRVdgfR4Qqrh$vzT*@;sQA)H!(BM0NHwUhjDR1l>h*!6*QRu literal 0 HcmV?d00001 diff --git a/mayhem/wat/testsuite/bool1.py b/mayhem/wat/testsuite/bool1.py new file mode 100755 index 0000000000..d7b06a4568 --- /dev/null +++ b/mayhem/wat/testsuite/bool1.py @@ -0,0 +1,10 @@ +def test_bool(): + print(True) + print(False) + b: bool + b = True + print(b) + b = False + print(b) + +test_bool() diff --git a/mayhem/wat/testsuite/expr14.py b/mayhem/wat/testsuite/expr14.py new file mode 100755 index 0000000000..413e1ed9a1 --- /dev/null +++ b/mayhem/wat/testsuite/expr14.py @@ -0,0 +1,12 @@ +def test_boolean_comparison(): + b1: bool + b2: bool + b3: bool + b1 = True + b2 = True + b3 = False + b1 = b1 > b2 + b1 = b1 == b2 + b1 = b2 != b3 + b1 = b2 >= b3 + b1 = b2 <= b3 diff --git a/mayhem/wat/testsuite/expr2.py b/mayhem/wat/testsuite/expr2.py new file mode 100755 index 0000000000..b26a96cc6e --- /dev/null +++ b/mayhem/wat/testsuite/expr2.py @@ -0,0 +1,11 @@ +def test_boolOp(): + a: bool + b: bool + a = False + b = True + a = a and b + b = a or True + a = a or b + a = a and b == b + a = a and b != b + a = b or b diff --git a/mayhem/wat/testsuite/print_str.py b/mayhem/wat/testsuite/print_str.py new file mode 100755 index 0000000000..41b2b7468c --- /dev/null +++ b/mayhem/wat/testsuite/print_str.py @@ -0,0 +1,13 @@ +def printCompOperations(): + print("x < y") + print("x !< y") + print("x > y") + print("x !> y") + print("x == y") + print("x != y") + + +def main0(): + printCompOperations() + +main0() \ No newline at end of file