hao 0.0.14

Read, edit, create and write .net assemblies
Documentation
mod common;

use std::process::Command;

use hao::{
    dotnet::{
        entries::{
            well_known::{SystemRuntimeInteropServicesType, SystemType},
            Field, Method, Param, TypeDef, Version,
        },
        il::{Instruction, MethodBody, OpCode, Operand},
        md::streams::tables_stream::{FieldFlags, MethodFlags, TypeAttributes},
        module::{ModuleKind, ModuleLoadOptions, TargetRuntime},
        signature::{FieldSig, MethodSig, TypeSig},
    },
    Module,
};

fn body(instructions: Vec<Instruction>) -> Option<MethodBody> {
    Some(MethodBody {
        instructions,
        ..Default::default()
    })
}

fn hello_world() -> Module {
    let mut module = Module::new("Hello", ModuleKind::Console, TargetRuntime::NetCore(9));
    module.add_assembly_ref(
        "System.Console",
        Version {
            major: 9,
            ..Default::default()
        },
        &TargetRuntime::NetCore(9).framework_token(),
    );

    let object = module.corlib_type(SystemType::Object).unwrap();
    let object_ctor = module
        .import_method_on(
            &object,
            ".ctor",
            MethodSig::new_instance(TypeSig::VOID, vec![]),
        )
        .unwrap();
    let console = module
        .import_type_from("System.Console", SystemType::Console)
        .unwrap();
    let write_line = module
        .import_method_on(
            &console,
            "WriteLine",
            MethodSig::new_static(TypeSig::VOID, vec![TypeSig::STRING]),
        )
        .unwrap();
    let string = module.corlib_type(SystemType::String).unwrap();
    let concat = module
        .import_method_on(
            &string,
            "Concat",
            MethodSig::new_static(TypeSig::STRING, vec![TypeSig::STRING, TypeSig::STRING]),
        )
        .unwrap();

    let public_class = TypeAttributes::Public | TypeAttributes::BeforeFieldInit;
    let greeter = module.add_type(TypeDef::new(
        public_class,
        "Hello",
        "Greeter",
        Some(object.clone()),
    ));
    let name = greeter.add_field(Field::new(
        FieldFlags::Private,
        "name",
        FieldSig {
            ty: TypeSig::STRING,
        },
    ));

    let ctor_flags = MethodFlags::Public
        | MethodFlags::HideBySig
        | MethodFlags::SpecialName
        | MethodFlags::RTSpecialName;
    let ctor = greeter.add_method(Method::new(
        ctor_flags,
        ".ctor",
        MethodSig::new_instance(TypeSig::VOID, vec![TypeSig::STRING]),
    ));
    {
        let mut c = ctor.get_mut();
        c.params
            .push(hao::dotnet::entries::Entry::new(Param::new(1, "name")));
        c.body = body(vec![
            Instruction::ldarg(0),
            Instruction::call(object_ctor),
            Instruction::ldarg(0),
            Instruction::ldarg(1),
            Instruction::new(OpCode::Stfld, Operand::Token(name.into())),
            Instruction::ret(),
        ]);
    }

    let greet = greeter.add_method(Method::new(
        MethodFlags::Public | MethodFlags::HideBySig,
        "Greet",
        MethodSig::new_instance(TypeSig::STRING, vec![]),
    ));
    greet.get_mut().body = body(vec![
        Instruction::ldstr("Hello, "),
        Instruction::ldarg(0),
        Instruction::new(
            OpCode::Ldfld,
            Operand::Token(greeter.get().fields[0].clone().into()),
        ),
        Instruction::call(concat),
        Instruction::ret(),
    ]);

    let program = module.add_type(TypeDef::new(
        TypeAttributes::Public | TypeAttributes::Abstract | TypeAttributes::Sealed,
        "Hello",
        "Program",
        Some(object),
    ));
    let main = program.add_method(Method::new(
        MethodFlags::Public | MethodFlags::Static | MethodFlags::HideBySig,
        "Main",
        MethodSig::new_static(TypeSig::VOID, vec![]),
    ));
    main.get_mut().body = body(vec![
        Instruction::ldstr("hao"),
        Instruction::newobj(ctor),
        Instruction::callvirt(greet),
        Instruction::call(write_line),
        Instruction::ret(),
    ]);
    module.entry_point = Some(main);
    module
}

#[test]
fn write_new_module() {
    let module = hello_world();
    let data = module.write().unwrap();

    let reloaded = Module::from_bytes(&data).unwrap();
    assert_eq!(reloaded.name(), "Hello.exe");
    assert_eq!(
        reloaded
            .find_method("Hello.Greeter", "Greet")
            .unwrap()
            .get()
            .name,
        "Greet"
    );
    assert_eq!(reloaded.entry_point.as_ref().unwrap().get().name, "Main");

    let dir = std::path::PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("new_module");
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("Hello.dll"), &data).unwrap();
    std::fs::write(
        dir.join("Hello.runtimeconfig.json"),
        r#"{"runtimeOptions":{"tfm":"net9.0","framework":{"name":"Microsoft.NETCore.App","version":"9.0.0"}}}"#,
    )
    .unwrap();
    let Ok(output) = Command::new("dotnet").arg(dir.join("Hello.dll")).output() else {
        eprintln!("skipping run: dotnet not installed");
        return;
    };
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(String::from_utf8_lossy(&output.stdout), "Hello, hao\n");
}

/// A new assembly calling a method defined in another loaded assembly.
#[test]
fn call_into_another_module() {
    let sample = sample!();
    let library = Module::from_bytes(&sample.bytes).unwrap();
    let add = library.find_method("Sample.Calc", "Add").unwrap();

    let mut module = Module::new("Caller", ModuleKind::Console, TargetRuntime::NetCore(9));
    module.add_assembly_ref(
        "System.Console",
        Version {
            major: 9,
            ..Default::default()
        },
        &TargetRuntime::NetCore(9).framework_token(),
    );
    let add = module.import_foreign_method(&add, &library).unwrap();
    let console = module
        .import_type_from("System.Console", SystemType::Console)
        .unwrap();
    let write_line = module
        .import_method_on(
            &console,
            "WriteLine",
            MethodSig::new_static(TypeSig::VOID, vec![TypeSig::INT32]),
        )
        .unwrap();
    let object = module.corlib_type(SystemType::Object).unwrap();

    let program = module.add_type(TypeDef::new(
        TypeAttributes::Public | TypeAttributes::Abstract | TypeAttributes::Sealed,
        "",
        "Program",
        Some(object),
    ));
    let main = program.add_method(Method::new(
        MethodFlags::Public | MethodFlags::Static,
        "Main",
        MethodSig::new_static(TypeSig::VOID, vec![]),
    ));
    main.get_mut().body = body(vec![
        Instruction::ldc_i4(40),
        Instruction::ldc_i4(2),
        Instruction::call(add),
        Instruction::call(write_line),
        Instruction::ret(),
    ]);
    module.entry_point = Some(main);
    assert!(module.find_assembly_ref("Sample").is_some());

    let dir = std::path::PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("caller");
    std::fs::create_dir_all(&dir).unwrap();
    module.write_to_path(dir.join("Caller.dll")).unwrap();
    std::fs::write(dir.join("Sample.dll"), &sample.bytes).unwrap();
    std::fs::copy(
        sample.dir.join("Sample.runtimeconfig.json"),
        dir.join("Caller.runtimeconfig.json"),
    )
    .unwrap();
    let output = Command::new("dotnet")
        .arg(dir.join("Caller.dll"))
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(String::from_utf8_lossy(&output.stdout), "42\n");
}

#[test]
fn builder_helpers() {
    use hao::dotnet::{
        entries::CustomAttributeCtor,
        il::{Emitter, HandlerKind},
    };

    let mut module = Module::new(
        "Helpers",
        ModuleKind::Dll,
        TargetRuntime::NetCoreImplementation(8),
    );
    let exports = module.add_static_class("Native", "Exports").unwrap();
    let uco = module
        .corlib_attribute(SystemRuntimeInteropServicesType::UnmanagedCallersOnlyAttribute)
        .unwrap();
    let exception = module.corlib_type(SystemType::Exception).unwrap();
    let max = module
        .import_corlib_method(
            SystemType::Math,
            "Max",
            MethodSig::new_static(TypeSig::INT32, vec![TypeSig::INT32; 2]),
        )
        .unwrap();

    // try { result = Math.Max(a, b); } catch (Exception) { result = -1; } return result;
    let mut e = Emitter::new();
    let result = e.declare_local(TypeSig::INT32);
    e.try_catch(
        exception,
        |e| {
            e.ldarg(0).ldarg(1).call(&max).stloc(result);
        },
        |e| {
            e.pop().ldc_i4(-1).stloc(result);
        },
    );
    e.ldloc(result).ret();
    exports.add_method(
        Method::public_static(
            "SafeMax",
            MethodSig::new_static(TypeSig::INT32, vec![TypeSig::INT32; 2]),
        )
        .with_attribute(uco)
        .with_body(e.finish()),
    );
    module.set_assembly_name("Helpers.Renamed");

    let reloaded = Module::from_bytes(&module.write().unwrap()).unwrap();
    assert_eq!(
        reloaded.assembly.as_ref().unwrap().get().name,
        "Helpers.Renamed"
    );
    let ty = reloaded.find_type("Native", "Exports").unwrap();
    assert!(ty.get().is_static() && ty.get().is_visible());
    let method = reloaded.find_method("Native.Exports", "SafeMax").unwrap();
    let method = method.get();
    assert!(method.is_public() && method.is_static());
    assert_eq!(
        method.custom_attributes[0].type_name(),
        "System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute"
    );
    assert!(matches!(
        method.custom_attributes[0].constructor,
        CustomAttributeCtor::MemberRef(_)
    ));
    let body = method.body.as_ref().unwrap();
    assert_eq!(body.exception_handlers.len(), 1);
    assert!(matches!(
        body.exception_handlers[0].kind,
        HandlerKind::Catch(_)
    ));
}

/// https://github.com/pigeonhands/hao/issues/3 — the last FieldRVA entry is a
/// static field of an external value type (`System.Guid`), so its initializer
/// size can't be determined without resolving mscorlib.
#[test]
fn field_rva_external_value_type_is_lenient() {
    fn repro() -> Module {
        let mut module = Module::new(
            "FieldRvaRepro",
            ModuleKind::Dll,
            TargetRuntime::NetFramework,
        );
        let owner = module.add_static_class("Repro", "Data").unwrap();
        let guid = module.corlib_type(SystemType::Guid).unwrap();
        let mut field = Field::new(
            FieldFlags::Public | FieldFlags::Static | FieldFlags::HasFieldRVA,
            "Value",
            FieldSig {
                ty: TypeSig::ValueType(guid),
            },
        );
        field.initial_value = Some(vec![0; 16]);
        owner.add_field(field);
        module
    }

    let bytes = repro().write().unwrap();

    // Lenient by default: the module opens, the un-sizable initializer skipped.
    let module = Module::from_bytes(&bytes).expect("lenient load should succeed");
    let data = module.find_type("Repro", "Data").expect("Repro.Data");
    let field = data.get().find_field("Value").expect("Value field");
    assert!(
        field.get().initial_value.is_none(),
        "un-sizable initializer should be skipped",
    );

    // Opt-out restores the hard error.
    let err = Module::from_bytes_with_options(
        &bytes,
        ModuleLoadOptions {
            strict_field_rva: true,
        },
    )
    .unwrap_err();
    assert!(
        format!("{err:?}").contains("FieldRVA data size"),
        "expected a FieldRVA size error, got: {err:?}",
    );
}