VeryUnsafe 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package VeryUnsafe --version 1.0.4
NuGet\Install-Package VeryUnsafe -Version 1.0.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="VeryUnsafe" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add VeryUnsafe --version 1.0.4
#r "nuget: VeryUnsafe, 1.0.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install VeryUnsafe as a Cake Addin
#addin nuget:?package=VeryUnsafe&version=1.0.4

// Install VeryUnsafe as a Cake Tool
#tool nuget:?package=VeryUnsafe&version=1.0.4

VeryUnsafe

This library contains a collection of various dangerous operations. Using unsafe may be less than necessary in some cases, mind your steps.

Disclaimer

Carefully read the documentation's remarks, for the operations are really unsafe in nature.

Features

  • GetObjectHandleAddress, to get the address of the type handle of an object
  • ChangeType, to change the type handle of an object at runtime, without requiring reallocation
  • ToAction extensions for Func delegates, using ChangeType

Usages

  • Pro-/demote objects at runtime
  • Mimic another type (may or may not be heavily inspired by Seb-stian/GetVoid)
  • Discard the return type of a delegate instance (inspired by a real world usage)
  • Corrupt the managed heap in one more way

Benchmarks

It's fast. Hella fast.

push      rax
mov       rax,[rcx+10]
mov       [rsp],rax
mov       rdx,[rsp]
mov       rcx,offset MT_VeryUnsafe.Benchmarks.ObjectPromotion+RecordB
mov       [rdx],rcx
add       rsp,8
ret

And, well, here are the results of a reference benchmark:

Method Mean Error StdDev Median Ratio Gen 0 Code Size Allocated
PromoteViaCopying 4.4002 ns 0.1016 ns 0.0900 ns 4.3919 ns 1.000 0.0057 54 B 24 B
PromoteUnsafe 0.0022 ns 0.0066 ns 0.0059 ns 0.0000 ns 0.001 - 31 B -

ObjectPromotion.PromoteUnsafe: Default → The method duration is indistinguishable from the empty method duration

To use or not to use

It's unsafe af. E. g. GC can move instances after we got the address but before we write to it. Or JIT doing some dirt that we broke the whole thing.

Some constructive feedback

Don't use it. (c) WhiteBlackGoose

I hate it (c) WhiteBlackGoose

<hr>

what I don't understand is why go further to try and make it "safer" because that can't happen (c) Tanner Gooding

there is no appeal, you can't make it safer (c) Tanner Gooding

its a runtime corrupting feature, so it can't be safe (c) Tanner Gooding

there is no "safer" (c) Tanner Gooding

that won't make this safer, you are still risking JIT corruption by lying about the type (c) Tanner Gooding

there is no real difference between "shoot yourself in the bare foot" and "shoot yourself in the foot with a sock on" (c) Tanner Gooding

<hr>

lgtm, can't wait to use it (c) Sebastian

Honestly I prefer my unsafe code safe<br> But I also love micro-optimizations, so it's hard for me to decide whether I absolutely love it or despise it (c) Sebastian

<hr>

Thanks, I hate it 😆 (c) Sergio

Stay safe!

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on VeryUnsafe:

Package Downloads
AdventOfCSharp

An aspiring AoC problem solving framework.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1 314 1/6/2023
1.1.0 431 5/7/2022
1.0.4 415 4/1/2022
1.0.3 1,544 3/16/2022
1.0.2 409 3/16/2022
1.0.1 325 9/5/2021
1.0.0 274 9/5/2021

1.0.4 changes:
- Rename namespace to Danger.VeryUnsafe (makes using it easier)
- Document breaking behavior when using ToAction on struct-returning delegates

1.0.3 changes:
- Remove required dependency to the source generator project

1.0.2 changes:
- Added convenient Func to Action extensions

1.0.1 changes:
- Much, much faster ChangeType<T> (credits to WhiteBlackGoose)
- ChangeType<T> now returns the new instance as of type T through Unsafe.As