Skip to content

Conversation

@vecchiot-aws
Copy link
Contributor

@vecchiot-aws vecchiot-aws commented Jul 19, 2021

Description of changes:

A PR introducing a --gen-c-runnable flag which produces a runnable C program from the compiled goto program. The existing --gen-c flag acts as a starting point, but there are a few issues listed in #237 that prevent the generated C file from compiling.

The change builds off of the visitor pattern introduced by #321, creating a symbol table transformer that normalizes identifiers and perform some extra processing on the symbol table.

(Replaces #317.)

Resolved issues:

Partially resolves #237 (possibly fully after more changes.)

Call-outs:

Todo:

  • Generate default bodies for nondet_<k>() functions.
  • Generate nondet_<k>() functions from a sample trace.
  • Verify definition for overflow
  • Fix field access in src/test/cbmc/FatPointers/boxtrait.c
  • Set up distinct flags for readable and runnable gen-c
  • Figure out testing strategy
  • Documentation
  • Clean up commits

Testing:

  • How is this change tested? Existing regression suite.

  • Is this a refactor change? No.

Checklist

  • Each commit message has a non-empty body, explaining why the change was made
  • Methods or procedures are documented
  • Regression or unit tests are included, or existing tests cover the modified code
  • My PR is restricted to a single feature or bugfix

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@vecchiot-aws vecchiot-aws mentioned this pull request Jul 19, 2021
12 tasks
@vecchiot-aws vecchiot-aws force-pushed the gen-c-transformer branch 2 times, most recently from 66a89f2 to 9d47f16 Compare July 20, 2021 20:07
@vecchiot-aws
Copy link
Contributor Author

vecchiot-aws commented Jul 21, 2021

To fix:

  • ForeignItems/main.rs
    • Static extern variables not declared
  • SizeAndAlignOfDst/main_fail.rs
    • __CPROVER_atomic_begin and __CPROVER_atomic_end
    • error: use of undeclared identifier '_ZN3std9panicking11panic_count18GLOBAL_PANIC_COUNT17hbb0e4a14d9617703E'
  • Waiting on CBMC fix
    • Parameter name matches local var
      • VolatileIntrinsics/main.rs
        • default is a keyword
        • Something to do with pointers
      • Strings/fixme_boxed_str.rs
        • Something to do with pointers
      • FatPointers/boxmuttrait.rs
        • byte_extract_little_endian defined wrong
        • Issue with parameter name matching local variable name.
      • Cast/from_be_bytes.rs
        • Something to do with overlapping parameter and local variable names?
      • Intrinsics/fixme_catch_unwind.rs
        • Something to do with overlapping parameter and local variable names?
      • Vectors/fixme_main.rs
    • Irep
      • BitManipulation/Stable/fixme_main_fail.rs
        • error: implicit declaration of function 'irep' is invalid in C99
      • BitManipulation/Unstable/Rotate/main.rs
        • error: implicit declaration of function 'irep' is invalid in C99
      • Vectors/fixme_main.rs
  • Done
    • SIMD/Operators/main.rs
      • error: assigning to 'long *' from incompatible type '__attribute__((__vector_size__(2 * sizeof(long)))) long'
      • error: address of vector element requested
    • DynTrait/*
      • error: indirection requires pointer operand ('struct _closure_DynTrait_boxed_closure_fail_fixme_rs_14_44__16_6_' invalid)
      • (This is caused by a bug which is currently being patched.)
    • CodegenConstValue/u128.rs
      • error: integer literal is too large to be represented in any integer type
    • PointerOffset/*
      • error: implicit declaration of function 'POINTER_OBJECT' is invalid in C99
    • SizeAndAlignOfDst/main.rs
      • __CPROVER_atomic_begin and __CPROVER_atomic_end
    • Atomics/*
      • __CPROVER_atomic_begin and __CPROVER_atomic_end
    • Cast/cast_abstract_args_to_concrete.rs
      • powi and powif not defined
    • CopyIntrinsics/main.rs
      • default is a keyword
      • Something to do with pointers
    • Asm/main_fixme.rs
      • No default value for type empty

@vecchiot-aws
Copy link
Contributor Author

Simplified version of error: assigning to 'long *' from incompatible type '__attribute__((__vector_size__(2 * sizeof(long)))) long'

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
#![feature(repr_simd)]

#[repr(simd)]
#[allow(non_camel_case_types)]
#[derive(PartialEq)]
pub struct i64x2(i64, i64);

fn main() {
    assert!(i64x2(0, 0) == i64x2(0, 0));
}

@vecchiot-aws vecchiot-aws force-pushed the gen-c-transformer branch 2 times, most recently from 14ad0a3 to d2a31b3 Compare July 26, 2021 20:35
@danielsn danielsn changed the base branch from main-153-2021-07-15 to main-153-2021-07-26 July 27, 2021 21:45
@vecchiot-aws
Copy link
Contributor Author

Changes:

  • Normalizing identifiers

    • Replace non-alphanumeric + "" + "$" with ""
    • Add "_" before name if starts with number
    • Add "_" to key words
    • If overlapping output names, add "_"
  • Transform names

    • Symbol name
    • Parameters
    • Member access to structs/unions
    • Symbol expr
    • Structs
      • Struct name
      • Struct component
      • Struct type
    • Unions
      • Union name
      • Union field
      • Union component
      • Union type
    • Goto/labels
  • Replacements

    • Big nums
      • If a constant goes above u64::MAX, break into (a * u64::MAX + b)
    • Bin ops
      • Replace lhs ==> rhs with !lhs || rhs
    • Vector indexing
      • Cast vector to array, and then index
    • Replace main with main_ so we can create main with int return type
  • Nondet

    • Replace nondet calls with calls to custom non_det functions with default return values
      • Except padding fields, since they are ignored, and ignored nondets are not genned in --dump-c
    • Fill in missing extern functions with calls to nondet
  • C library

    • Manually define some CPROVER stuff

@vecchiot-aws vecchiot-aws force-pushed the gen-c-transformer branch 2 times, most recently from 1ee7132 to db93ae4 Compare August 4, 2021 18:19
@vecchiot-aws vecchiot-aws changed the base branch from main-153-2021-07-26 to main-154-2021-08-02 August 4, 2021 19:33
@vecchiot-aws vecchiot-aws changed the base branch from main-154-2021-08-02 to main-154-2021-08-17 August 19, 2021 15:56
@vecchiot-aws vecchiot-aws force-pushed the gen-c-transformer branch 2 times, most recently from d3f43fe to a446058 Compare August 19, 2021 22:52
@vecchiot-aws vecchiot-aws marked this pull request as ready for review August 19, 2021 22:53
@danielsn danielsn merged commit 2d8367a into model-checking:main-154-2021-08-17 Aug 20, 2021
tedinski pushed a commit to tedinski/rmc that referenced this pull request Apr 26, 2022
carolynzech pushed a commit to carolynzech/kani that referenced this pull request May 14, 2025
This function was added in model-checking#334; all its callers are gone now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Output of gen-c is not valid C code

2 participants