FractalFir / rustc_codegen_clr

This rust compiler backend(module) emmits valid CIL (.NET IR), enabling you to use Rust in .NET projects.
MIT License
1.39k stars 30 forks source link

[Linker issue] Latest commit fails to compile example in QUICKSTART.md #40

Open u9g opened 3 months ago

u9g commented 3 months ago

Describe the bug Latest commit fails to compile example in QUICKSTART.md,

error:

> cargo run
warning: unused manifest key: build
warning: unused manifest key: unstable
   Compiling c v0.1.0 (C:\Users\myusername\Documents\code\5-22-24\c)
error: linking with `\\?\C:\Users\myusername\Documents\code\rustc_codegen_clr\target\debug\linker.exe` failed: exit code: 0xc0000135
  |
  = note: "\\\\?\\C:\\Users\\myusername\\Documents\\code\\rustc_codegen_clr\\target\\debug\\linker.exe" "/NOLOGO" "C:\\Users\\myusername\\AppData\\Local\\Temp\\rustcAsZO3H\\symbols.o" "C:\\Users\\myusername\\Documents\\code\\5-22-24\\c\\target\\debug\\deps\\c.bc" "/LIBPATH:C:\\Users\\myusername\\Documents\\code\\5-22-24\\c\\target\\debug\\deps" "/LIBPATH:C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-8ac42145e0282c2a.rlib" "C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libpanic_unwind-50670a58b21a47e0.rlib" "C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_demangle-175d80d3c3db88b7.rlib" "C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd_detect-07381e2f40221155.rlib" "C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libhashbrown-c83e267e89319fe0.rlib" "C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_alloc-6fd6593ddc25dc10.rlib" "C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libunwind-27634911093fa9aa.rlib" "C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcfg_if-60c84f47f1803ffa.rlib" "C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-7bfd0a6d1a588dbd.rlib" "C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_core-cca5aeb939a92213.rlib" "C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-0a7d9524c210bd3d.rlib" "C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-d827b00cff8216d8.rlib" "kernel32.lib" "advapi32.lib" "kernel32.lib" "ntdll.lib" "userenv.lib" "ws2_32.lib" "kernel32.lib" "ws2_32.lib" "kernel32.lib" "/defaultlib:msvcrt" "/NXCOMPAT" "/LIBPATH:C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "/OUT:C:\\Users\\myusername\\Documents\\code\\5-22-24\\c\\target\\debug\\deps\\c.exe" "/OPT:REF,NOICF" "/DEBUG" "/PDBALTPATH:%_PDB%" "/NATVIS:C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:C:\\Users\\myusername\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libstd.natvis" "--cargo-support"
  = note:

error: could not compile `c` (bin "c") due to 1 previous error

Example of miscompiled code

extern "C" {
    fn puts(msg: *const u8);
}
fn main() {
    // A heap-allocated string!
    let mut string = String::with_capacity(100);
    string.push('H');
    string.push('e');
    string.push('l');
    string.push('l');
    string.push('o');
    string.push('.');
    string.push('\n');
    string.push('\0');
    unsafe { puts(string.as_ptr()) };
    // String literals work too.
    unsafe { puts("Rust + NET = <3!\n\0".as_ptr()) };
}

Expected behavior It should run normally.

Info about your system (please complete the following information):

Additional context None

FractalFir commented 3 months ago

Can you run ilasm from the command prompt? Does your target folder contain a file ending in .il?

u9g commented 3 months ago

doesn't look like it https://gist.github.com/u9g/79ffa3fec5a1999097b4ad9cff549733

PS C:\Users\myusername\Documents\code\rustc_codegen_clr> cd .\target\
PS C:\Users\myusername\Documents\code\rustc_codegen_clr\target> ls

    Directory: C:\Users\myusername\Documents\code\rustc_codegen_clr\target

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         5/22/2024   4:14 PM                debug
-a----         5/22/2024   9:15 PM           1901 .rustc_info.json
-a----         5/22/2024  12:35 PM            177 CACHEDIR.TAG

PS C:\Users\myusername\Documents\code\rustc_codegen_clr\target> cd .\debug\
PS C:\Users\myusername\Documents\code\rustc_codegen_clr\target\debug> ls

    Directory: C:\Users\myusername\Documents\code\rustc_codegen_clr\target\debug

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         5/22/2024   4:13 PM                .fingerprint
d-----         5/22/2024  12:35 PM                build
d-----         5/22/2024   4:14 PM                deps
d-----         5/22/2024  12:35 PM                examples
d-----         5/22/2024   4:13 PM                incremental
-a----         5/22/2024  12:35 PM              0 .cargo-lock
-a----         5/22/2024   4:13 PM           4205 librustc_codegen_clr.d
-a----         5/22/2024   4:14 PM      862147402 librustc_codegen_clr.rlib
-a----         5/22/2024   4:13 PM           4630 linker.d
-a----         5/22/2024   4:14 PM        1499648 linker.exe
-a----         5/22/2024   4:14 PM       24596480 linker.pdb
-a----         5/22/2024   4:13 PM           4201 rustc_codegen_clr.d
-a----         5/22/2024   4:14 PM        2833408 rustc_codegen_clr.dll
-a----         5/22/2024   4:14 PM            935 rustc_codegen_clr.dll.exp
-a----         5/22/2024   4:13 PM           1978 rustc_codegen_clr.dll.lib
-a----         5/22/2024   4:14 PM       29446144 rustc_codegen_clr.pdb
-a----         5/22/2024   4:13 PM           4262 rustflags.d
-a----         5/22/2024   4:14 PM          89088 rustflags.exe
-a----         5/22/2024   4:14 PM       18010112 rustflags.pdb
FractalFir commented 3 months ago

Could you try running cargo run --bin rustflags using the newest commit? It should check if the codegen can use ilasm.

Besides that, the .il file should be somewhere in the target directory of the crate you are building, not within the target directory of rustc_codegen_clr. Could you search for the .il file there?

u9g commented 3 months ago

Hm, I just installed ilasm

PS C:\Users\myusername\Documents\code\rustc_codegen_clr> cargo run --bin rustflags
warning: unused imports: `BinOp` and `Operand`    
   |
13 | use rustc_middle::mir::{BinOp, Operand};
   |                         ^^^^^  ^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: `rustc_codegen_clr` (lib) generated 1 warning (run `cargo fix --lib -p rustc_codegen_clr` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s
     Running `target\debug\rustflags.exe`
Welcome to the `rustc_codegen_clr` environment setup helper!
This tool will help you use the codegen to compile Rust projects.

Doing dependency checks...
thread 'main' panicked at src\bin\rustflags.rs:4:19:
Could not find the CIL assembler at name/path "ilasm", due to Error { kind: NotFound, message: "program not found" }. 
Please instal the CIL assembler, and/or set the ILASM_PATH enviroment variable to point to your CIL assembler.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\rustflags.exe` (exit code: 101)
PS C:\Users\myusername\Documents\code\rustc_codegen_clr> cargo run --bin rustflags
warning: unused imports: `BinOp` and `Operand`
  --> src\binop\checked\mod.rs:13:25
   |
13 | use rustc_middle::mir::{BinOp, Operand};
   |                         ^^^^^  ^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: `rustc_codegen_clr` (lib) generated 1 warning (run `cargo fix --lib -p rustc_codegen_clr` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s
     Running `target\debug\rustflags.exe`
Welcome to the `rustc_codegen_clr` environment setup helper!
This tool will help you use the codegen to compile Rust projects.

Doing dependency checks...
An CIL assembler has been detected.
Dependency checks succeded.

WARNING: Please note, the project is currently in the early stages of development.  Bugs, crashes and miscompilations will occur.
Currently, there is no way to permanently install the codegen. It is enabled on a per-shell-session basis (enabled for your particular command prompt window).

In order to compile cargo crates with `rustc_codegen_clr`, please set the RUSTFLAGS environment variable to:

"-Z codegen-backend=\\?\C:\Users\myusername\Documents\code\rustc_codegen_clr\target\debug\rustc_codegen_clr.dll -C linker=\\?\C:\Users\myusername\Documents\code\rustc_codegen_clr\target\debug\linker.exe -C link-args=--cargo-support "

You may use the following command to quickly set the required environment variables:

$Env:RUSTFLAGS = '-Z codegen-backend=\\?\C:\Users\myusername\Documents\code\rustc_codegen_clr\target\debug\rustc_codegen_clr.dll -C linker=\\?\C:\Users\myusername\Documents\code\rustc_codegen_clr\target\debug\linker.exe -C link-args=--cargo-support '   

After you are done working with `rustc_codegen_clr` either unset the environment variable OR restart your shell (close the command prompt window).

Please note that those variables may change when the codegen is updated/rebuilt.
After each time the codegen is rebuilt, please use this tool again to get updated build environment variables.

If you are using the project, please remember to:
1. Update BOTH rustc and the project on a regular basis.
2. Report compiler bugs to the maintainers of `rustc_codegen_clr`, and not the maintainers of the Rust compiler as a whole.
  In 99.999% of the cases, the bug is within this project and not the Rust compiler.

Looks like it detected ilasm but still has the same problem

Looks like there aren't any .il files either. image

FractalFir commented 3 months ago

The newest version of the project should (hopefully) fix the issue.

If the issue still persists, could you try running the linker manually (by going to the cilly directory, and running cargo run)?