vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.78k stars 2.16k forks source link

"builder error / C error. This should never happen.", when calling C.xcb_setup_roots_iterator() from XCB library #16906

Open Durobot opened 1 year ago

Durobot commented 1 year ago

Describe the bug

Compilation of the following code that uses XCB library leads to "builder error / C error. This should never happen.":

module main

#flag -lxcb

// This structure is not defined anywhere in XCB
// header files. Only forward declarations exist,
// which means you are not supposed to create instances
// of it manually, or modify its fields. All you can do
// is pass around pointers to Xcb_connection_t.
// Since V doesn't have anything similar to forward
// declaration of structs, this is the best I can do.
struct Xcb_connection_t {} //{ ham int }

type Xcb_keycode_t = u8

struct Xcb_setup_t
{
    status                      u8
    pad0                        u8
    protocol_major_version      u16
    protocol_minor_version      u16
    length                      u16
    release_number              u32
    resource_id_base            u32
    resource_id_mask            u32
    motion_buffer_size           u32
    vendor_len                  u16
    maximum_request_length      u16
    roots_len                   u8
    pixmap_formats_len          u8
    image_byte_order            u8
    bitmap_format_bit_order     u8
    bitmap_format_scanline_unit u8
    bitmap_format_scanline_pad  u8
    min_keycode                 Xcb_keycode_t
    max_keycode                 Xcb_keycode_t
    pad1                        [4]u8
}

struct Xcb_screen_iterator_t
{
    data  &Xcb_screen_t
    rem   int
    index int
}

type Xcb_window_t = u32
type Xcb_colormap_t = u32
type Xcb_visualid_t = u32

struct Xcb_screen_t
{
    root                  Xcb_window_t
    default_colormap      Xcb_colormap_t
    white_pixel           u32
    black_pixel           u32
    current_input_masks   u32
    width_in_pixels       u16
    height_in_pixels      u16
    width_in_millimeters  u16
    height_in_millimeters u16
    min_installed_maps    u16
    max_installed_maps    u16
    root_visual           Xcb_visualid_t
    backing_stores        u8
    save_unders           u8
    root_depth            u8
    allowed_depths_len    u8
}

fn C.xcb_connect(displayname &i8, screenp &int) &Xcb_connection_t
fn C.xcb_connection_has_error(c &Xcb_connection_t) int
fn C.xcb_setup_roots_iterator(r &Xcb_setup_t) Xcb_screen_iterator_t
fn C.xcb_get_setup(c &Xcb_connection_t) &Xcb_setup_t
fn C.xcb_disconnect(c &Xcb_connection_t)

fn main()
{
    println('Trying to connect to the X server with XCB...')
    unsafe
    {
        mut p_xcb_con := C.xcb_connect(0, 0) // NULL, NULL for defaults
        if C.xcb_connection_has_error(p_xcb_con) != 0
        {
            println('Error opening connection')
            return
        }
        println('Connection established')

        // Trying to get X server properties
        // xcb_setup_t*
        p_xcb_setup := // Must comment out for -prod build - unused var
            C.xcb_get_setup(p_xcb_con) // if the line below is also commented out

        // Get root windows iterator, but use only the first root
        // window (on the firat screen), since it has to exist on
        // all systems (multi-monitor or not).
        // Xcb_screen_iterator_t
        scr_iter := C.xcb_setup_roots_iterator(p_xcb_setup)

        // xcb_screen_t*
        //p_xcb_screen := scr_iter.data
        //println("Screen 0 dimensions: ${p_xcb_screen.width_in_pixels} x ${p_xcb_screen.height_in_pixels}")

        C.xcb_disconnect(p_xcb_con)
        println('Connection closed')
    }
}

The error appears only when this line scr_iter := C.xcb_setup_roots_iterator(p_xcb_setup) is present in the code.

Expected Behavior

Successful compilation and execution

Current Behavior

Here's the error message when building with v . -cg:

/tmp/v_1000/v_xcb_basic.14691998837476445534.tmp.c:12197: warning: implicit declaration of function 'xcb_connect'
/tmp/v_1000/v_xcb_basic.14691998837476445534.tmp.c:12197: warning: assignment makes pointer from integer without a cast
/tmp/v_1000/v_xcb_basic.14691998837476445534.tmp.c:12197: warning: cast between pointer and integer of different size
/tmp/v_1000/v_xcb_basic.14691998837476445534.tmp.c:12198: warning: implicit declaration of function 'xcb_connection_has_error'
/tmp/v_1000/v_xcb_basic.14691998837476445534.tmp.c:12203: warning: implicit declaration of function 'xcb_get_setup'
/tmp/v_1000/v_xcb_basic.14691998837476445534.tmp.c:12203: warning: assignment makes pointer from integer without a cast
/tmp/v_1000/v_xcb_basic.14691998837476445534.tmp.c:12203: warning: cast between pointer and integer of different size
/tmp/v_1000/v_xcb_basic.14691998837476445534.tmp.c:12204: warning: implicit declaration of function 'xcb_setup_roots_iterator'
/tmp/v_1000/v_xcb_basic.14691998837476445534.tmp.c:12204: error: '{' expected (got ";")
builder error: 
==================
C error. This should never happen.

Reproduction Steps

v . or v . -cg on a Linux machine with XCB library installed.

Possible Solution

No response

Additional Information/Context

If the offending line (scr_iter := C.xcb_setup_roots_iterator(p_xcb_setup)) is commented out, the program builds and runs. However, when it is built with -prod key, it segfaults:

$ ./v_xcb_basic 
Trying to connect to the X server with XCB...
signal 11: segmentation fault
                                                        | 0x560d3e9414bc | ./v_xcb_basic(+0x24bc) 
                                                        | 0x7f4509466290 | /usr/lib/libc.so.6(+0x23290) 
                                                        | 0x7f450946634a | /usr/lib/libc.so.6(__libc_start_main+0x8a) 
                                                        | 0x560d3e941555 | ./v_xcb_basic(+0x2555)

V version

Current V version: V 0.3.2 a60f34e, timestamp: 2023-01-07 12:04:04 +0200

Environment details (OS name and version, etc.)

$ v doctor OS: linux, Linux version 6.1.3-arch1-1 (linux@archlinux) (gcc (GCC) 12.2.0, GNU ld (GNU Binutils) 2.39.0) #1 SMP PREEMPT_DYNAMIC Wed, 04 Jan 2023 16:28:15 +0000 Processor: 4 cpus, 64bit, little endian, Intel(R) Pentium(R) CPU 4415Y @ 1.60GHz CC version: cc (GCC) 12.2.0

getwd: /home/archie/projects/xcb-playground/v_xcb_basic vmodules: /home/archie/.vmodules vroot: /home/archie/projects/v vexe: /home/archie/projects/v/v vexe mtime: 2023-01-07 13:48:01 is vroot writable: true is vmodules writable: true V full version: V 0.3.2 ef5be22.a60f34e

Git version: git version 2.39.0 Git vroot status: weekly.2022.46-284-ga60f34e6 .git/config present: true thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

felipensp commented 7 months ago

Can you try again with a recent repo version?