DEV Community

Zero Heartbeat
Zero Heartbeat

Posted on Originally published at delta1labs.com

Why renaming survives source-generated COM

Open a .NET assembly that exports a classic [ComVisible] COM object in a decompiler and the method names are right there — they have to be, because a COM client can late-bind through IDispatch and ask for "Add" by name at runtime. Obfuscation and COM have an old, uneasy relationship for exactly this reason: the wire contract is partly made of your identifiers. So it is worth knowing that the modern .NET COM path breaks that link entirely. An interface marked [GeneratedComInterface] can have every one of its names renamed to a single letter and the native contract does not move.

The real question

What actually travels across a COM boundary — the method name, or the method's position? The answer decides whether renaming is safe, and it is different for the two COM paths .NET gives you.

The classic path, [ComVisible(true)] plus a type library, carries names. A late-bound client calls IDispatch::GetIDsOfNames("Add") to turn the string into a dispatch id, then Invokes it. Rename Add to a and that lookup fails. The source-generated path introduced in .NET 8, [GeneratedComInterface], carries nothing but a GUID and an ordinal. It builds a classic COM vtable at compile time through ComWrappers, and dispatch is purely positional: the caller queries for the interface by its IID, gets a pointer to a table of function pointers, and calls the one at a fixed slot. No string is ever consulted. That single difference is why renaming is free on one path and fatal on the other.

The mechanism, shown

Here is a minimal source-generated COM interface and its implementation. The source generator requires both to be partial and the interface to carry a [Guid].

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("5C13C3D8-4C4C-4F2B-9E1A-6B3A2F8E9D01")]
public partial interface ICalculator
{
    int Add(int a, int b);
    int Subtract(int a, int b);
}

[GeneratedComClass]
public partial class Calculator : ICalculator
{
    public int Add(int a, int b) => a + b;
    public int Subtract(int a, int b) => a - b;
}
Enter fullscreen mode Exit fullscreen mode

ComInterfaceGenerator runs at compile time and emits the plumbing: a managed-to-unmanaged stub (so your managed code can call a native object through this interface) and an unmanaged-to-managed vtable (so native code can call your Calculator). The part that matters for obfuscation is how each side addresses a method. Here is the generated managed-to-unmanaged call for Add, abbreviated to its essence:

// source-generated, abbreviated
int ICalculator.Add(int a, int b)
{
    var vtable = ((IUnmanagedVirtualMethodTableProvider)this)
        .GetVirtualMethodTableInfoForKey(typeof(ICalculator));
    int result;
    // slot 3 = the three IUnknown slots (0,1,2) + method index 0
    int hr = ((delegate* unmanaged<void*, int, int, int*, int>)vtable.Table[3])(
        vtable.ThisPtr, a, b, &result);
    Marshal.ThrowExceptionForHR(hr);
    return result;
}
Enter fullscreen mode Exit fullscreen mode

The only identity on the wire is the IID — typeof(ICalculator) keys a table whose entry 3 is Add. The literal 3 is Add's address. The string "Add" appears nowhere in the generated call; the native side on the other end of that function pointer does not know or care what the method is named. Subtract is slot 4, and so on.

That is the whole insight, drawn out:

Now obfuscate the assembly. Nebula renames ICalculator to A, Calculator to B, Add to a, Subtract to b. The source-generated stub and vtable are ordinary IL in the same module, so they are renamed in the same pass by the same map — the stub that was ICalculator.Add becomes A.a, still indexing vtable.Table[3], still keyed by typeof(A). The IID attribute blob is untouched: Nebula renames metadata identifiers, it does not rewrite custom-attribute values, so [Guid("5C13C3D8-…")] and the [GeneratedComInterface] marker ride through unchanged. A native host that was compiled against IID 5C13C3D8-… queries for that IID, gets the vtable, calls slot 3, and runs a — the method formerly known as Add. Nothing on the native side sees the rename because the native side never saw a name.

How it applies

The practical upshot is that source-generated COM is one of the few framework-integrated surfaces you can hand to the obfuscator with almost no keep-list. The names are genuinely internal. You do, however, have to protect three things the renamer does not touch but other transforms might.

The IID. The [Guid] on the interface is the COM contract identity. Nebula preserves attribute values by default, so this is safe unless you deliberately enable an attribute-stripping pass; if you do, scope it so it never removes [Guid] or [GeneratedComInterface]/[GeneratedComClass] from these types.

Member order. The vtable layout is positional, so reordering interface methods renumbers every slot after the change — a binary break against any already-compiled native caller. Renaming never reorders members, so default obfuscation is fine; the rule is simply never to let any member-reordering transform touch a [GeneratedComInterface].

Reachability. This is the one case that needs a keep rule, and it is about pruning, not renaming. If ICalculator is public, preservePublicApi keeps it reachable and you need nothing. But if the interface is internal and is only ever called from native code across the COM boundary, the obfuscator's reachability analysis sees no managed caller and may treat Add and Subtract as dead methods eligible for removal. Pin them:

{
  "schemaVersion": 1,
  "inputs": [ "bin/Release/net8.0/Acme.Interop.dll" ],
  "outputDirectory": "protected",

  "encryptStrings": true,
  "controlFlowObfuscation": true,

  "renaming": {
    "rules": [
      { "pattern": "Acme.Interop.*", "action": "rename" }
    ]
  },
  "pruning": {
    "rules": [
      { "pattern": "Acme.Interop.ICalculator", "action": "keep", "members": "all" },
      { "pattern": "Acme.Interop.Calculator",  "action": "keep", "members": "all" }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Note what this does not do: it keeps the types from being pruned, but it leaves them in the rename ruleset, so their names still change. You are protecting reachability while still hiding every identifier — exactly the split that makes this surface cheap to obfuscate.

Verify it the way you verify any keep decision: open the protected assembly in Glass.NET and confirm the interface and class read as A and B with mangled members, that the [Guid("5C13C3D8-…")] attribute is intact on the interface, and that the two methods still exist in declaration order. Then run the native host against the protected build, because a COM contract break is invisible at compile time and obvious the instant the host calls a slot that moved.

When it breaks

The free-renaming guarantee holds only for the source-generated, vtable-dispatched path. It evaporates the moment a name re-enters the contract.

If the same object is also exposed as [ComVisible(true)] for an IDispatch scripting client — an Office add-in, a VBScript host, an automation consumer — that client binds by name through GetIDsOfNames, and renaming the dispatched members breaks it. A type that straddles both worlds has to keep the names its IDispatch consumers use; see obfuscating a COM-visible assembly for that path's rules. Source-generated COM is IDispatch-free precisely to avoid this, so the clean case is an interface used only through [GeneratedComInterface].

Two assemblies that both declare the same [GeneratedComInterface] to talk to each other must agree on the IID and the slot order, not the names — so you can obfuscate each with a different rename seed and they still interoperate, as long as neither reorders members and both keep the shared [Guid]. And if you marshal the interface across the boundary by its IID with a hand-written QueryInterface, that IID must match the attribute value you shipped; never let an attribute-stripping configuration remove it.

Takeaway

Classic COM made your method names part of the wire contract, which is why obfuscating it has always meant a keep-list. The .NET 8 [GeneratedComInterface] path trades name-based IDispatch for a compile-time ComWrappers vtable, and a vtable is addressed by position, not by string. So the one thing obfuscation is best at — erasing identifiers — costs you nothing here: rename the interface, the class and every method, keep the IID, the member order and reachability, and the native contract never notices.

Top comments (0)