Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when extern function item has no return type #595

Closed
philberty opened this issue Jul 26, 2021 · 6 comments · Fixed by #596 or #598
Closed

Crash when extern function item has no return type #595

philberty opened this issue Jul 26, 2021 · 6 comments · Fixed by #596 or #598
Assignees
Labels

Comments

@philberty
Copy link
Member

philberty commented Jul 26, 2021

extern "C" {
    fn puts(s: *const i8);
}

fn main() {
    unsafe {
        let a = "foo\0";
        let b = a as *const str;
        let c = b as *const i8;
        puts(c);
    }
}
Program received signal SIGSEGV, Segmentation fault.                                                                                                                                                                                                   
Rust::AST::ExternalFunctionItem::as_string[abi:cxx11]() const (this=0x3d35f50) at ../../gccrs/gcc/rust/ast/rust-ast-full-test.cc:3598                                                                                                                  
3598      str += "\n (return) Type: " + return_type->as_string ();                                                                                                                                                                                     
(gdb) bt                                                                                                                   
#0  Rust::AST::ExternalFunctionItem::as_string[abi:cxx11]() const (this=0x3d35f50) at ../../gccrs/gcc/rust/ast/rust-ast-full-test.cc:3598
#1  0x0000000000e9e12a in Rust::AST::ExternBlock::as_string[abi:cxx11]() const (this=0x3cfc820) at ../../gccrs/gcc/rust/ast/rust-ast-full-test.cc:1316                                                                                                 
#2  0x0000000000e9875e in Rust::AST::Crate::as_string[abi:cxx11]() const (this=0x7fffffffd4e0) at ../../gccrs/gcc/rust/ast/rust-ast-full-test.cc:197                                                                                                   
#3  0x0000000000f2940e in Rust::Parser<Rust::Lexer>::debug_dump_ast_output (this=0x7fffffffd5d0, crate=..., out=...) at ../../gccrs/gcc/rust/parse/rust-parse-impl.h:14890                                                                             
#4  0x0000000000f0ea55 in Rust::Session::dump_ast (this=0x3b87820 <session>, parser=..., crate=...) at ../../gccrs/gcc/rust/rust-session-manager.cc:852                                                                                                
#5  0x0000000000f0d3fe in Rust::Session::parse_file (this=0x3b87820 <session>, filename=0x7fffffffe029 "test.rs") at ../../gccrs/gcc/rust/rust-session-manager.cc:494                                                                                  
#6  0x0000000000f0d1fb in Rust::Session::parse_files (this=0x3b87820 <session>, num_files=1, files=0x3d47a20) at ../../gccrs/gcc/rust/rust-session-manager.cc:458
#7  0x0000000000e68265 in grs_langhook_parse_file () at ../../gccrs/gcc/rust/rust-lang.cc:171
#8  0x00000000018bb23b in compile_file () at ../../gccrs/gcc/toplev.c:457
#9  0x00000000018be509 in do_compile () at ../../gccrs/gcc/toplev.c:2201
#10 0x00000000018be838 in toplev::main (this=0x7fffffffdad6, argc=18, argv=0x7fffffffdbe8) at ../../gccrs/gcc/toplev.c:2340
#11 0x0000000002ac7c7d in main (argc=18, argv=0x7fffffffdbe8) at ../../gccrs/gcc/main.c:39

Originally posted by @philberty in #421 (comment)

@philberty philberty self-assigned this Jul 26, 2021
@philberty philberty added the bug label Jul 26, 2021
@philberty philberty added this to the Data Structures 3 - Traits milestone Jul 26, 2021
bors bot added a commit that referenced this issue Jul 26, 2021
596: Fix crash when extern function item has no return type r=philberty a=philberty

Addresses #421
Fixes #595


Co-authored-by: Philip Herron <[email protected]>
@bors bors bot closed this as completed in bb351c7 Jul 26, 2021
@bors bors bot closed this as completed in #596 Jul 26, 2021
@bugaevc
Copy link
Contributor

bugaevc commented Jul 26, 2021

I'm still getting an error with 5b3909d when trying to compile this example:

$ bin/gccrs test.rs 
Preparing to parse files. 
Attempting to parse file: test.rs
beginning null denotation identifier handling
current peek token when starting path pratt parse: 'as'
current token (just about to return path to null denotation): 'as'
finished null denotation identifier path parsing - next is branching 
beginning null denotation identifier handling
current peek token when starting path pratt parse: 'as'
current token (just about to return path to null denotation): 'as'
finished null denotation identifier path parsing - next is branching 
beginning null denotation identifier handling
current peek token when starting path pratt parse: '('
current token (just about to return path to null denotation): '('
finished null denotation identifier path parsing - next is branching 
beginning null denotation identifier handling
current peek token when starting path pratt parse: ')'
current token (just about to return path to null denotation): ')'
finished null denotation identifier path parsing - next is branching 
SUCCESSFULLY PARSED CRATE 
ran register_plugins (with no body)
SUCCESSFULLY REGISTERED PLUGINS 
started injection
finished injection
SUCCESSFULLY FINISHED INJECTION 
started expansion
finished expansion
SUCCESSFULLY FINISHED EXPANSION 
test.rs:6:5: fatal error: Failed to lower expr: [UnsafeBlockExpr:
  outer attributes: noneBlockExpr:

  outer attributes: none
  inner attributes: none
 statements: 
 
  outer attributes: none
 let a = foo
 
  outer attributes: none
 let b = a as *const str
 
  outer attributes: none
 let c = b as *const i8
 ExprStmtWithoutBlock:
  CallExpr: 
   outer attributes: none
 Function expr: puts
 Call params:
  c
 final expression: none

]
    6 |     unsafe {
      |     ^
compilation terminated

@philberty
Copy link
Member Author

I'm still getting an error with 5b3909d when trying to compile this example:

$ bin/gccrs test.rs 
Preparing to parse files. 
Attempting to parse file: test.rs
beginning null denotation identifier handling
current peek token when starting path pratt parse: 'as'
current token (just about to return path to null denotation): 'as'
finished null denotation identifier path parsing - next is branching 
beginning null denotation identifier handling
current peek token when starting path pratt parse: 'as'
current token (just about to return path to null denotation): 'as'
finished null denotation identifier path parsing - next is branching 
beginning null denotation identifier handling
current peek token when starting path pratt parse: '('
current token (just about to return path to null denotation): '('
finished null denotation identifier path parsing - next is branching 
beginning null denotation identifier handling
current peek token when starting path pratt parse: ')'
current token (just about to return path to null denotation): ')'
finished null denotation identifier path parsing - next is branching 
SUCCESSFULLY PARSED CRATE 
ran register_plugins (with no body)
SUCCESSFULLY REGISTERED PLUGINS 
started injection
finished injection
SUCCESSFULLY FINISHED INJECTION 
started expansion
finished expansion
SUCCESSFULLY FINISHED EXPANSION 
test.rs:6:5: fatal error: Failed to lower expr: [UnsafeBlockExpr:
  outer attributes: noneBlockExpr:

  outer attributes: none
  inner attributes: none
 statements: 
 
  outer attributes: none
 let a = foo
 
  outer attributes: none
 let b = a as *const str
 
  outer attributes: none
 let c = b as *const i8
 ExprStmtWithoutBlock:
  CallExpr: 
   outer attributes: none
 Function expr: puts
 Call params:
  c
 final expression: none

]
    6 |     unsafe {
      |     ^
compilation terminated

Is this with the testcase from the issue above?

@philberty philberty reopened this Jul 26, 2021
@bugaevc
Copy link
Contributor

bugaevc commented Jul 26, 2021

Yes.

But this might be the same issue as #421, so perhaps I just spoke too soon 🙂

@philberty
Copy link
Member Author

I wonder are you able to run any of the expected pass testcases for unsafe over in:

https://github.com/Rust-GCC/gccrs/blob/master/gcc/testsuite/rust/compile/torture/unsafe1.rs

Maybe something else is going on if you have the latest code it should be lowering the unsafe to HIR at the moment.

@bugaevc
Copy link
Contributor

bugaevc commented Jul 27, 2021

With 5b3909d, no, it gives a similar error:

$ bin/gccrs ../src/gcc/testsuite/rust/compile/torture/unsafe1.rs 
Preparing to parse files. 
Attempting to parse file: ../src/gcc/testsuite/rust/compile/torture/unsafe1.rs
beginning null denotation identifier handling
current peek token when starting path pratt parse: '='
current token (just about to return path to null denotation): '='
finished null denotation identifier path parsing - next is branching 
beginning null denotation identifier handling
current peek token when starting path pratt parse: '}'
current token (just about to return path to null denotation): '}'
finished null denotation identifier path parsing - next is branching 
beginning null denotation identifier handling
current peek token when starting path pratt parse: '='
current token (just about to return path to null denotation): '='
finished null denotation identifier path parsing - next is branching 
beginning null denotation identifier handling
current peek token when starting path pratt parse: '('
current token (just about to return path to null denotation): '('
finished null denotation identifier path parsing - next is branching 
SUCCESSFULLY PARSED CRATE 
ran register_plugins (with no body)
SUCCESSFULLY REGISTERED PLUGINS 
started injection
finished injection
SUCCESSFULLY FINISHED INJECTION 
started expansion
finished expansion
SUCCESSFULLY FINISHED EXPANSION 
../src/gcc/testsuite/rust/compile/torture/unsafe1.rs:2:5: fatal error: Failed to lower expr: [UnsafeBlockExpr:
  outer attributes: noneBlockExpr:

  outer attributes: none
  inner attributes: none
 statements: 
 
  outer attributes: none
 let a
 ExprStmtWithoutBlock:
  AssignmentExpr: 
 left: a
 right: 123
 final expression: 
a

]
    2 |     unsafe {
      |     ^
compilation terminated.

But with b992c80 the testcase now builds! 🎉

The puts example still runs into an error:

$ bin/gccrs test.rs 
test.rs:10:9: error: reverse lookup failure
   10 |         puts(c);
      |         ^
test.rs:10:9: error: failed to type resolve expression
test.rs:10:9: error: Failed to resolve expression of function call
test.rs:10:9: error: failed to type resolve expression

@philberty
Copy link
Member Author

Ah yes, this PR #598 fixes that. I am just having some trouble with dejagnu for this test which I am working through before I merge it.

@philberty philberty linked a pull request Jul 27, 2021 that will close this issue
@bors bors bot closed this as completed in #598 Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants