Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 103 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ jobs:
- name: Install Python dependencies
run: pip install tomli

- name: Install gdb (Linux)
if: runner.os == 'Linux'
run: sudo apt-get install -y gdb

- name: Enable core dumps (Linux)
if: runner.os == 'Linux'
run: |
sudo mkdir -p /cores
sudo chmod 1777 /cores
sudo sysctl -w kernel.core_pattern='/cores/core.%e.%p.%t'
sudo sysctl -w kernel.core_uses_pid=1
sudo systemctl stop apport.service 2>/dev/null || true
sudo systemctl stop apport.socket 2>/dev/null || true
cat /proc/sys/kernel/core_pattern

- name: Configure
run: |
mkdir build && cd build
Expand All @@ -50,10 +65,96 @@ jobs:
run: ninja
working-directory: build

- name: Run unit tests
run: ninja check
- name: Run unit tests (loop until segfault or cap)
working-directory: build
shell: bash
env:
REPRO_MAX_ITERATIONS: "50"
run: |
set +e
ulimit -c unlimited 2>/dev/null || true
shopt -s nullglob
MAX="${REPRO_MAX_ITERATIONS}"
last_rc=0
# core_pattern is /cores/core.%e.%p.%t, where %e is the executable
# name truncated to 15 chars (TASK_COMM_LEN-1). UB tests compile to
# Rust binaries named test_ub<N>_<model> (truncated to test_ub<N>_<x>)
# and may segfault by design - those are expected and get discarded.
# Anything else (cpp2rust itself, or a non-UB test binary) is a real
# crash we want to capture.
is_expected_ub_core() {
local base exe
base=$(basename "$1")
exe=${base#core.}
exe=${exe%%.*}
[[ $exe == test_ub* ]]
}
for i in $(seq 1 "$MAX"); do
echo "::group::iteration $i / $MAX"
ninja check-unit
last_rc=$?
echo "::endgroup::"
if [[ "$RUNNER_OS" == "Linux" ]]; then
real_cores=()
for c in /cores/core.*; do
if is_expected_ub_core "$c"; then
echo "ignoring expected UB-test core: $(basename "$c")"
rm -f "$c"
else
real_cores+=( "$c" )
fi
done
if (( ${#real_cores[@]} > 0 )); then
echo "got a real core on iteration $i (check-unit rc=$last_rc)"
printf '%s\n' "${real_cores[@]}"
exit 1
fi
fi
if (( last_rc != 0 )); then
echo "iteration $i failed with rc=$last_rc but no real core captured - continuing"
fi
done
# Mirror original semantics: fail the step if the last run failed,
# even without a core dump.
exit $last_rc

- name: Generate backtraces from cores (Linux)
if: failure() && runner.os == 'Linux'
shell: bash
run: |
set -x
ls -la /cores || true
shopt -s nullglob
for core in /cores/core.*; do
echo "=== $core ==="
gdb -batch \
-ex "set pagination off" \
-ex "thread apply all bt full" \
-ex "info registers" \
-ex "info sharedlibrary" \
./cpp2rust/cpp2rust "$core" 2>&1 | tee "${core}.bt.txt" || true
done
working-directory: build

- name: Package binary + cores for local gdb (Linux)
if: failure() && runner.os == 'Linux'
shell: bash
run: |
mkdir -p /tmp/repro-bundle
cp -v ./build/cpp2rust/cpp2rust /tmp/repro-bundle/ || true
find ./build -maxdepth 4 -name 'libcc2rs*' -exec cp -v {} /tmp/repro-bundle/ \; || true
cp -v /cores/core.* /tmp/repro-bundle/ 2>/dev/null || true
ls -la /tmp/repro-bundle

- name: Upload coredump bundle
if: failure() && runner.os == 'Linux'
uses: actions/upload-artifact@v4
with:
name: segfault-repro-${{ matrix.os }}-${{ matrix.build_type }}-${{ github.run_id }}-${{ github.run_attempt }}
path: /tmp/repro-bundle/**
if-no-files-found: warn
retention-days: 7

- name: Check rules
run: ninja check-rules
working-directory: build
Expand Down
6 changes: 6 additions & 0 deletions cpp2rust/converter/mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ std::unordered_map<std::string, TranslationRule::TypeTgt>
clang::PrintingPolicy getPrintPolicy() {
assert(ctx_);
clang::PrintingPolicy policy(ctx_->getLangOpts());
policy.Bool = true;
policy.SuppressTagKeyword = true;
policy.SuppressScope = false;
policy.FullyQualifiedName = true;
Expand Down Expand Up @@ -562,6 +563,11 @@ std::string normalizeTranslationRule(std::string rule) {

} // namespace

PushASTContext::PushASTContext(clang::ASTContext &ctx) : prev_(ctx_) {
ctx_ = &ctx;
}
PushASTContext::~PushASTContext() { ctx_ = prev_; }

bool Contains(clang::QualType qual_type) {
return search(qual_type) != types_.end();
}
Expand Down
12 changes: 12 additions & 0 deletions cpp2rust/converter/mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#include <clang/AST/ASTContext.h>
#include <clang/AST/Expr.h>
#include <clang/AST/Type.h>

Expand All @@ -14,6 +15,17 @@
#include "converter/translation_rule.h"

namespace cpp2rust::Mapper {
class PushASTContext {
public:
explicit PushASTContext(clang::ASTContext &ctx);
~PushASTContext();
PushASTContext(const PushASTContext &) = delete;
PushASTContext &operator=(const PushASTContext &) = delete;

private:
clang::ASTContext *prev_;
};

bool Contains(clang::QualType qual_type);
bool Contains(const clang::Expr *expr);

Expand Down
1 change: 1 addition & 0 deletions cpp2rust/converter/translation_rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {

void run(const clang::ast_matchers::MatchFinder::MatchResult &R) override {
assert(sema_);
Mapper::PushASTContext scoped(*R.Context);
if (auto var = R.Nodes.getNodeAs<clang::TypeAliasDecl>("tvar")) {
clang::QualType type;
if (auto *tdecl = var->getDescribedAliasTemplate()) {
Expand Down
Loading