Skip to content

(c2rust-analyze) Handle transpiled string literals (b"...\0" as *const u8 as *const libc::c_char) #833

Open
@kkysen

Description

@kkysen

Currently, c2rust-analyze can't handle c2rust transpiled string literals. String literals like "literal" in C become b"literal\0" as *const u8 as *const libc::c_char in Rust, but the ptr-to-ptr cast of different pointer types is not handled, and this happens:

b"\0" as *const u8 as *const libc::c_char;
visit_statement(_19 = const b"\x00")
thread 'rustc' panicked at 'unexpected pointer type in &[u8; 1]', c2rust-analyze/src/context.rs:503:9
stack backtrace:
   0: rust_begin_unwind
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/panicking.rs:142:14
   2: c2rust_analyze::context::label_no_pointers::{{closure}}
             at ./src/context.rs:503:9
   3: c2rust_analyze::labeled_ty::LabeledTyCtxt<L>::label
             at ./src/labeled_ty.rs:157:21
   4: c2rust_analyze::context::label_no_pointers
             at ./src/context.rs:502:5
   5: <rustc_middle::mir::syntax::Operand as c2rust_analyze::context::TypeOf>::type_of
             at ./src/context.rs:494:41
   6: <&T as c2rust_analyze::context::TypeOf>::type_of
             at ./src/context.rs:464:9
   7: c2rust_analyze::context::AnalysisCtxt::type_of
             at ./src/context.rs:241:9
   8: c2rust_analyze::context::AnalysisCtxt::type_of_rvalue
             at ./src/context.rs:304:36
   9: c2rust_analyze::dataflow::type_check::TypeChecker::visit_statement
             at ./src/dataflow/type_check.rs:233:30
  10: c2rust_analyze::dataflow::type_check::visit
             at ./src/dataflow/type_check.rs:388:13
  11: c2rust_analyze::dataflow::generate_constraints
             at ./src/dataflow/mod.rs:326:5
  12: c2rust_analyze::run
             at ./src/main.rs:480:45
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

We want to at the very least special-case string literals, as they're extremely common, but we should be able to generalize a little bit more than that without making it more complex. That is, we should allow casts from *T to *U if T and U are both integer types of the same size, such as u8 and c_char (i8 or u8, but usually i8).

Depends on fixing:

There is an RFC for c"" strings in Rust, which would make transpilation of C string literals much simpler, as well as this issue, but that RFC is recent and there is no implementation coming soon:

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions