zig icon indicating copy to clipboard operation
zig copied to clipboard

cInclude "openssl/evp.h" generates un-callable code

Open eduardvercaemer opened this issue 1 year ago • 1 comments

Zig Version

0.12.0

Steps to Reproduce and Observed Behavior

// main.zig (link with libcrypto)
const c = @cImport({
    @cInclude("openssl/evp.h");
});


pub fn main() !void {
    c.OpenSSL_add_all_digests();
}

It looks like cInclude is generating some types that are too strict ?

install
+- install hashes
   +- zig build-exe hashes Debug native 1 errors
/storage/dev/hashes/zig-cache/o/3f9c8c6974de1ce3504f556ff87dd6c6/cimport.zig:19872:99: error: expected type '?*const cimport.struct_ossl_init_settings_st', found '?*anyopaque'
/storage/dev/hashes/zig-cache/o/3f9c8c6974de1ce3504f556ff87dd6c6/cimport.zig:19872:99: note: pointer type child 'anyopaque' cannot cast into pointer type child 'cimport.struct_ossl_init_settings_st'
/storage/dev/hashes/zig-cache/o/3f9c8c6974de1ce3504f556ff87dd6c6/cimport.zig:652:42: note: opaque declared here
/storage/dev/hashes/zig-cache/o/3f9c8c6974de1ce3504f556ff87dd6c6/cimport.zig:2743:56: note: parameter type declared here
error: the following command failed with 1 compilation errors:
/usr/bin/zig build-exe -lcrypto -ODebug -Mroot=/storage/dev/hashes/src/main.zig -lc --cache-dir /storage/dev/hashes/zig-cache --global-cache-dir /home/ed/.cache/zig --name hashes --listen=- 
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
+- install hashes transitive failure
   +- zig build-exe hashes Debug native 1 errors
error: the following build command failed with exit code 1:
/storage/dev/hashes/zig-cache/o/aea757a3ca70ee4b5e891e293ec7456c/build /usr/bin/zig /storage/dev/hashes /storage/dev/hashes/zig-cache /home/ed/.cache/zig --seed 0xd81d78fe -Zad192e9daeff7e60

Expected Behavior

The code should compile.

Notes

It looks like if I replace the call with the shown code inside of Type directly I can get it to work:

const c = @cImport({
    @cInclude("openssl/evp.h");
});

pub fn main() !void {
    _ = c.OPENSSL_init_crypto(c.OPENSSL_INIT_ADD_ALL_DIGESTS, null);
}

eduardvercaemer avatar May 09 '24 03:05 eduardvercaemer

I think translate-c might be at some point assigning a NULL constant the type ?*anyopaque. This typed null requires a @ptrCast to coerce to a function pointer type, which translate-c fails to add. (Which would make it fundamentally the same issue as in #19911).

(I'm not sure at which point we type NULL, but if it's too early we might benefit from introducing a separate type for the NULL constant.)

rohlem avatar May 09 '24 09:05 rohlem