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

Nested functions in gcc are not working #3083

Closed
fgssfgss opened this issue Apr 6, 2018 · 2 comments
Closed

Nested functions in gcc are not working #3083

fgssfgss opened this issue Apr 6, 2018 · 2 comments

Comments

@fgssfgss
Copy link

fgssfgss commented Apr 6, 2018

  • Your Windows build number:
    Microsoft Windows [Version 10.0.16299.309]

  • What you're doing and what's happening:
    I've wrote C code with lambda function.

#include <stdio.h>

#define LAMBDA(c_) ({ c_ _;})

void funct2(void (*f)()) {
        f();
}

void func() {
        funct2(LAMBDA(void _() {
                printf("Hello world\n");
        }));
        return;
}

int main() {
        func();
        return 0;
}

This code successfully compiles in WSL using gcc, in real Ubuntu 16.04 too.
But when I try to run it, I get this:

user@z510-laptop:~/work$ gcc -z execstack -o lambda lambda.c
user@z510-laptop:~/work$ ./lambda
Segmentation fault (core dumped)
  • What's wrong / what should be happening instead:
    Seems like stack memory is not executable, but on Ubuntu it is executable.
    And as far as I know gcc using executable stack for trampoline code for nested functions.
    On real Ubuntu or any other linux it runs successfully and echoes "Hello world":
user@ubuntu:~$ gcc -z execstack -o lambda lambda.c
user@ubuntu:~$ ./lambda
Hello World
  • Strace of the failing command, if applicable:
user@z510-laptop:~/work$ strace -ff ./lambda
execve("./lambda", ["./lambda"], [/* 17 vars */]) = 0
brk(NULL)                               = 0x891000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=28837, ...}) = 0
mmap(NULL, 28837, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f6bbe1e6000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\t\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1868984, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f6bbe1e0000
mmap(NULL, 3971488, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f6bbda30000
mprotect(0x7f6bbdbf0000, 2097152, PROT_NONE) = 0
mmap(0x7f6bbddf0000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7f6bbddf0000
mmap(0x7f6bbddf6000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f6bbddf6000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f6bbe1d0000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f6bbe1c0000
arch_prctl(ARCH_SET_FS, 0x7f6bbe1d0700) = 0
mprotect(0x7f6bbddf0000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ)     = 0
mprotect(0x7f6bbe025000, 4096, PROT_READ) = 0
munmap(0x7f6bbe1e6000, 28837)           = 0
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_ACCERR, si_addr=0x7fffe4fc0e80} ---
+++ killed by SIGSEGV (core dumped) +++
Segmentation fault (core dumped)
@fgssfgss
Copy link
Author

fgssfgss commented Apr 6, 2018

GDB stacktrace:

(gdb) r
Starting program: /home/user/work/lambda

Program received signal SIGSEGV, Segmentation fault.
0x00007ffffffde030 in ?? ()
(gdb) bt
#0  0x00007ffffffde030 in ?? ()
#1  0x00000000004005ad in funct2 (f=0x7ffffffde030) at lambda.c:6
#2  0x0000000000400612 in func () at lambda.c:10
#3  0x0000000000400637 in main () at lambda.c:17
(gdb)

And stack info from /proc/< pid >/maps:

7fffff7df000-7ffffffdf000 rw-p 00000000 00:00 0 [stack]

@therealkenc
Copy link
Collaborator

#286 #916 #2562 #2553 (message) etc etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants