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

[Bug] Access violation on Windows builds #165

Closed
junglie85 opened this issue Jun 1, 2023 · 4 comments
Closed

[Bug] Access violation on Windows builds #165

junglie85 opened this issue Jun 1, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@junglie85
Copy link

Describe the bug

Running a simple publisher on Windows fails with an access violation accessing location 0xFF whereas the exact same program on WSL (Ubuntu) runs as expected.

To reproduce

Start a subscriber:

In WSL, build the zenoh-c library as per the repo instructions then compile the following program:

#include <stdio.h>
#include <string.h>

#include "zenoh.h"

int main(int argc, char **argv)
{
    printf("Running...\n");

    char *keyexpr = "demo/example/zenoh";
    char *value = "Hello Zenoh!";

    printf("A\n");
    z_owned_config_t config = z_config_default();
    if (zc_config_insert_json(z_loan(config), Z_CONFIG_CONNECT_KEY, "[\"tcp/localhost:7447\"]") < 0)
    {
        printf("Could not add endpoint to config");
        exit(-1);
    }
    printf("B\n");

    z_owned_session_t session = z_open(z_move(config));
    if (!z_check(session))
    {
        printf("Unable to open session");
        exit(-1);
    }

    z_owned_publisher_t publisher = z_declare_publisher(z_loan(session), z_keyexpr(keyexpr), nullptr);
    if (!z_check(publisher))
    {
        printf("Unable to declare publisher");
        exit(-1);
    }

    z_publisher_put_options_t options = z_publisher_put_options_default();
    z_publisher_put(z_loan(publisher), (const uint8_t *)value, strlen(value), &options);

    z_undeclare_publisher(z_move(publisher));
    z_close(z_move(session));

    printf("Done\n");

    return 0;
}

Build and run:

gcc main.cpp -L$(pwd) -lzenohc -o zc
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd) ./zc

See output and message in subscriber:

Running...
A
B
Done

Repeat, but in Windows:

cl.exe main.cpp /o zc.exe /link zenohc.dll.lib
zc.exe
Running...
A

System info

  • Windows 10 (native and WSL Ubuntu 22.04).
  • zenoh-c 57d69f0
@junglie85 junglie85 added the bug Something isn't working label Jun 1, 2023
@junglie85
Copy link
Author

I've discovered that if I replace Z_CONFIG_CONNECT_KEY with the expected value "connect/endpoints" it does not crash.

@milyin
Copy link
Contributor

milyin commented Sep 10, 2023

@junglie85, thank you for the report! Reproduced the issue in https://github.com/eclipse-zenoh/zenoh-c/tree/pub_crash_on_windows branch (z_crash example), investigating

@milyin
Copy link
Contributor

milyin commented Sep 11, 2023

Problem doesn't happen if examples are built with static zenoh library:

cmake -Szenoh-c -Bbuild -DZENOHC_BUILD_EXAMPLES_WITH_STATIC_LIB=TRUE

But for dynamic library case the problem exists:
static linking:

Z_CONFIG_CONNECT_KEY=00007FF787FE7381
*Z_CONFIG_CONNECT_KEY=connect/endpoints

dynamic linking:

Z_CONFIG_CONNECT_KEY=CCCC0000CA5F25FF
(crash)

Seems like the way how it is exported from rust is incorrect:

#[no_mangle]
pub static Z_CONFIG_CONNECT_KEY: &c_char =
    unsafe { &*(b"connect/endpoints\0".as_ptr() as *const c_char) };

@milyin
Copy link
Contributor

milyin commented Sep 12, 2023

Problem occurs on all constants, not only on string ones. This code:

    printf("Z_ROUTER = %d\n", Z_ROUTER);
    printf("Z_PEER = %d\n", Z_PEER);
    printf("Z_CLIENT = %d\n", Z_CLIENT);

also outputs wrong values:

Z_ROUTER = -829610497
Z_PEER = -835312129
Z_CLIENT = -835443201

The working solution is to add __declspec(dllimport) to declarations. After this change:

__declspec(dllimport) extern const unsigned int Z_ROUTER;
__declspec(dllimport) extern const unsigned int Z_PEER;
extern const unsigned int Z_CLIENT;

the output is

Z_ROUTER = 1
Z_PEER = 2
Z_CLIENT = -854252033

Here are some information which may help:
rust-lang/rust#27438
https://www.reddit.com/r/rust/comments/169yeei/linkage_to_dll/
https://rust-lang.github.io/rfcs/2627-raw-dylib-kind.html

It may be also make sense to consider raw-dylib option for windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants