Skip to content

Convert nethost to C - #129828

Merged
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c
Jul 2, 2026
Merged

Convert nethost to C#129828
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c

Conversation

@elinor-fung

@elinor-fungelinor-fung commented Jun 25, 2026

Copy link
Copy Markdown
Member

Convert nethost to C. This builds on #128420 and #129480 to fully convert nethost so it compiles and links as C-only — no C++ runtime dependency.

cc @dotnet/appmodel @AaronRobinsonMSFT

  • pal
    • Add a C pal_get_loaded_library (look up an already-loaded library without loading it). The C++ pal::get_loaded_library now delegates to it.
  • fxr_resolver
    • Convert the last C++-only piece, try_get_existing_fxr, to C.
    • Remove the thin C++ wrappers in fxr_resolver.cpp
    • Move load_fxr_and_get_delegate (used by comhost and ijwhost) into its own header load_fxr_and_get_delegate.h
  • nethost
    • Replace nethost.cpp with nethost.c using the shared host bits that have been converted to C. The RAII error_writer_scope_t is replaced by manual save/restore of the error writer.
  • Build
    • Split hostmisc into C/C++ sources and add a C-only hostmisc_c object library

linux x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a2,743,470532,650-2,210,820-80.6%
libnethost.so43,37635,072-8,304-19.1%

macOS arm64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a1,097,312268,360−828,952−75.5%
libnethost.dylib92,36889,088−3,280−3.6%

windows x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.lib2,951,1761,677,420−1,273,756−43.2%
nethost.dll71,16856,832−14,336−20.1%

Note

This PR description was drafted by GitHub Copilot.

elinor-fungand others added 5 commits June 23, 2026 14:51
Add a C implementation of looking up an already-loaded library in the
current process, so callers that need to reuse an existing hostfxr (e.g.
nethost) can do so without the C++ pal:: surface.
- Add a pal_dll_t typedef and pal_get_loaded_library declaration in pal.h.
- Implement pal_get_loaded_library in pal.unix.c (dlopen RTLD_NOLOAD,
dladdr to resolve the path, and the /proc/self/maps fallback) and
pal.windows.c (GetModuleHandleW + a shared get_module_file_name helper
factored out of pal_get_own_executable_path).
- Repoint the C++ pal::get_loaded_library wrappers at the new C function,
removing the old C++ implementations and the proc-maps helper.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Now that pal_get_loaded_library is available in C, move the last
fxr_resolver function that was still C++-only into fxr_resolver.c.
Add fxr_resolver_try_get_existing_fxr (declaration in fxr_resolver.h,
implementation in fxr_resolver.c) and make the C++
fxr_resolver::try_get_existing_fxr wrapper delegate to it. Drop the file
comment noting that try_get_existing_fxr had to stay in C++.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Rewrite nethost.cpp as nethost.c using the C host APIs: trace_* for
tracing, fxr_resolver_try_get_existing_fxr / try_get_path_from_dotnet_root
/ try_get_path for resolution, and utils_get_directory. The RAII
error_writer_scope_t is replaced by saving and restoring the previous
error writer manually around the call, and pal::string_t buffers become
manually-managed pal_char_t* allocations.
Update nethost/CMakeLists.txt to build nethost.c. The compiled nethost
code is now C-only.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fxr_resolver.cpp held thin C++ wrappers (marshalling pal::string_t to and
from the pal_char_t* C API) over the fxr_resolver_* functions in
fxr_resolver.c. Remove them and have the remaining C++ callers use the C
API directly:
- hostfxr_resolver.cpp uses fxr_search_location and fxr_resolver_try_get_path
directly (with direct utils.h/error_codes.h includes that were previously
pulled in transitively through fxr_resolver.h).
- The load_fxr_and_get_delegate template (used by ijwhost and comhost) moves
to its own header, load_fxr_and_get_delegate.h, and calls the C resolver
functions. Its fxr handle is typed as pal_dll_t.
fxr_resolver.h is now a pure C header and fxr_resolver is C-only (no
fxr_resolver.cpp), which also drops it from the static singlefilehost
sources.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
nethost's own code is C, and after the previous change fxr_resolver is
C-only too, but nethost still linked the full hostmisc (which includes C++
translation units). That gave the nethost targets a C++ linker language, so
the clang++ driver added a dependency on the C++ runtime (libc++) and
libnethost.a embedded the unused C++ objects.
Split the hostmisc sources into C_SOURCES/CXX_SOURCES and add hostmisc_c, an
object library built from just the C sources. hostmisc and hostmisc::public
are unchanged for the C++ consumers (apphost, hostfxr, hostpolicy, etc.).
Point nethost/libnethost at hostmisc_c (+ minipal_objects, which object
libraries don't propagate transitively). The nethost targets now compile no
C++, link with the C driver, and have no libc++ dependency; libnethost.a
shrinks substantially by dropping the embedded C++ objects.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR completes the nethost conversion to C-only by rewriting nethost in C, moving remaining fxr_resolver functionality to the C surface, and adding a C PAL helper to detect an already-loaded hostfxr without introducing a C++ runtime dependency.

Changes:

  • Replaced nethost.cpp with nethost.c, switching to the C host APIs and manually saving/restoring the trace error writer.
  • Added C pal_get_loaded_library (and pal_dll_t) and routed the existing C++ pal::get_loaded_library through it.
  • Removed fxr_resolver.cpp and moved the delegate-loading template to load_fxr_and_get_delegate.h; updated C++ callers to use the C fxr_resolver_* APIs directly.
Show a summary per file
FileDescription
src/native/corehost/nethost/nethost.cppRemoved C++ implementation of get_hostfxr_path.
src/native/corehost/nethost/nethost.cNew C implementation of get_hostfxr_path using C host APIs.
src/native/corehost/nethost/CMakeLists.txtSwitched nethost sources to C and updated link deps to hostmisc_c.
src/native/corehost/load_fxr_and_get_delegate.hNew header containing the load_fxr_and_get_delegate C++ template previously in fxr_resolver.h.
src/native/corehost/ijwhost/ijwhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/comhost/comhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/hostmisc/pal.hAdded pal_dll_t and the C pal_get_loaded_library declaration.
src/native/corehost/hostmisc/pal.windows.cAdded pal_get_loaded_library implementation and refactored module-path helper.
src/native/corehost/hostmisc/pal.windows.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/pal.unix.cAdded pal_get_loaded_library implementation (dlopen RTLD_NOLOAD + dladdr, with /proc/self/maps fallback).
src/native/corehost/hostmisc/pal.unix.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/CMakeLists.txtSplit hostmisc C vs C++ sources and introduced hostmisc_c object library.
src/native/corehost/fxr_resolver.hRemoved C++ wrapper surface; added C fxr_resolver_try_get_existing_fxr.
src/native/corehost/fxr_resolver.cppRemoved thin C++ wrapper implementation.
src/native/corehost/fxr_resolver.cAdded C implementation of fxr_resolver_try_get_existing_fxr.
src/native/corehost/CMakeLists.txtUpdated fxr_resolver interface target to stop compiling fxr_resolver.cpp.
src/native/corehost/apphost/static/CMakeLists.txtDropped ../../fxr_resolver.cpp from sources.
src/native/corehost/apphost/standalone/hostfxr_resolver.cppSwitched from C++ wrappers to C fxr_resolver_try_get_path API.
src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.csUpdated comment to match renamed enum reference (fxr_search_location).

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 4

Comment threadsrc/native/corehost/nethost/nethost.c
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/fxr_resolver.c Outdated
Comment threadsrc/native/corehost/fxr_resolver.c
- nethost.c: use %zu (not %d) for the size_t in the invalid-size trace.
- pal.unix.c: bound the /proc/self/maps %s read with an explicit field
width so a pathological line can't overflow the buffer.
- fxr_resolver.c: initialize *out_fxr in fxr_resolver_try_get_existing_fxr,
and drop the stale fxr_resolver.cpp reference from the file comment.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
It only held a c_fx_ver_t, so pass the c_fx_ver_t directly as the
pal_readdir_onlydirectories context instead of wrapping it.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 25, 2026 20:26

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings June 25, 2026 20:38

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/nethost/CMakeLists.txt
Comment threadsrc/native/corehost/apphost/standalone/hostfxr_resolver.cpp Outdated
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
CopilotAI review requested due to automatic review settings June 30, 2026 15:27
In hostfxr_resolver_t's constructor, wrap the dotnet_root/fxr_path raw
pointers in a block so they go out of scope right after they are freed,
preventing later dangling-pointer use. The rest of the constructor only
needs the resolved flag and the assigned members.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 30, 2026 15:35

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 2

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/fxr_resolver.h Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings July 1, 2026 20:06

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
Co-authored-by: Elinor Fung <elfung@microsoft.com>
CopilotAI review requested due to automatic review settings July 1, 2026 22:48

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

@elinor-fung
elinor-fung merged commit ad0692a into dotnet:mainJul 2, 2026
173 checks passed
@elinor-fung
elinor-fung deleted the nethost-to-c branch July 2, 2026 17:13
@dotnet-milestone-botdotnet-milestone-botBot added this to the 11.0-preview7 milestone Jul 3, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
Convert `nethost` to C. This builds on #128420 and #129480 to fully
convert `nethost` so it compiles and links as C-only — no C++ runtime
dependency.
- **pal**
- Add a C `pal_get_loaded_library` (look up an already-loaded library
without loading it). The C++ `pal::get_loaded_library` now delegates to
it.
- **fxr_resolver** - Convert the last C++-only piece, `try_get_existing_fxr`, to C.
- Remove the thin C++ wrappers in `fxr_resolver.cpp`
- Move `load_fxr_and_get_delegate` (used by `comhost` and `ijwhost`)
into its own header `load_fxr_and_get_delegate.h`
- **nethost**
- Replace `nethost.cpp` with `nethost.c` using the shared host bits that
have been converted to C. The RAII `error_writer_scope_t` is replaced by
manual save/restore of the error writer.
- **Build**
- Split `hostmisc` into C/C++ sources and add a C-only `hostmisc_c`
object library
**linux x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---|---|---|---|
| `libnethost.a` | 2,743,470 | 532,650 | -2,210,820 | -80.6% |
| `libnethost.so` | 43,376 | 35,072 | -8,304 | -19.1% |
**macOS arm64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---:|---:|---:|---:|
| `libnethost.a` | 1,097,312 | 268,360 | −828,952 | −75.5% |
| `libnethost.dylib` | 92,368 | 89,088 | −3,280 | −3.6% |
**windows x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|--:|--:|--:|--:|
| `libnethost.lib` | 2,951,176 | 1,677,420 | −1,273,756 | −43.2% |
| `nethost.dll` | 71,168 | 56,832 | −14,336 | −20.1% |
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Aug 3, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

@elinor-fung@AaronRobinsonMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Convert nethost to C by elinor-fung · Pull Request #129828 · dotnet/runtime · GitHub
Skip to content

Convert nethost to C - #129828

Merged
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c
Jul 2, 2026
Merged

Convert nethost to C#129828
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c

Conversation

@elinor-fung

@elinor-fungelinor-fung commented Jun 25, 2026

Copy link
Copy Markdown
Member

Convert nethost to C. This builds on #128420 and #129480 to fully convert nethost so it compiles and links as C-only — no C++ runtime dependency.

cc @dotnet/appmodel @AaronRobinsonMSFT

  • pal
    • Add a C pal_get_loaded_library (look up an already-loaded library without loading it). The C++ pal::get_loaded_library now delegates to it.
  • fxr_resolver
    • Convert the last C++-only piece, try_get_existing_fxr, to C.
    • Remove the thin C++ wrappers in fxr_resolver.cpp
    • Move load_fxr_and_get_delegate (used by comhost and ijwhost) into its own header load_fxr_and_get_delegate.h
  • nethost
    • Replace nethost.cpp with nethost.c using the shared host bits that have been converted to C. The RAII error_writer_scope_t is replaced by manual save/restore of the error writer.
  • Build
    • Split hostmisc into C/C++ sources and add a C-only hostmisc_c object library

linux x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a2,743,470532,650-2,210,820-80.6%
libnethost.so43,37635,072-8,304-19.1%

macOS arm64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a1,097,312268,360−828,952−75.5%
libnethost.dylib92,36889,088−3,280−3.6%

windows x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.lib2,951,1761,677,420−1,273,756−43.2%
nethost.dll71,16856,832−14,336−20.1%

Note

This PR description was drafted by GitHub Copilot.

elinor-fungand others added 5 commits June 23, 2026 14:51
Add a C implementation of looking up an already-loaded library in the
current process, so callers that need to reuse an existing hostfxr (e.g.
nethost) can do so without the C++ pal:: surface.
- Add a pal_dll_t typedef and pal_get_loaded_library declaration in pal.h.
- Implement pal_get_loaded_library in pal.unix.c (dlopen RTLD_NOLOAD,
dladdr to resolve the path, and the /proc/self/maps fallback) and
pal.windows.c (GetModuleHandleW + a shared get_module_file_name helper
factored out of pal_get_own_executable_path).
- Repoint the C++ pal::get_loaded_library wrappers at the new C function,
removing the old C++ implementations and the proc-maps helper.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Now that pal_get_loaded_library is available in C, move the last
fxr_resolver function that was still C++-only into fxr_resolver.c.
Add fxr_resolver_try_get_existing_fxr (declaration in fxr_resolver.h,
implementation in fxr_resolver.c) and make the C++
fxr_resolver::try_get_existing_fxr wrapper delegate to it. Drop the file
comment noting that try_get_existing_fxr had to stay in C++.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Rewrite nethost.cpp as nethost.c using the C host APIs: trace_* for
tracing, fxr_resolver_try_get_existing_fxr / try_get_path_from_dotnet_root
/ try_get_path for resolution, and utils_get_directory. The RAII
error_writer_scope_t is replaced by saving and restoring the previous
error writer manually around the call, and pal::string_t buffers become
manually-managed pal_char_t* allocations.
Update nethost/CMakeLists.txt to build nethost.c. The compiled nethost
code is now C-only.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fxr_resolver.cpp held thin C++ wrappers (marshalling pal::string_t to and
from the pal_char_t* C API) over the fxr_resolver_* functions in
fxr_resolver.c. Remove them and have the remaining C++ callers use the C
API directly:
- hostfxr_resolver.cpp uses fxr_search_location and fxr_resolver_try_get_path
directly (with direct utils.h/error_codes.h includes that were previously
pulled in transitively through fxr_resolver.h).
- The load_fxr_and_get_delegate template (used by ijwhost and comhost) moves
to its own header, load_fxr_and_get_delegate.h, and calls the C resolver
functions. Its fxr handle is typed as pal_dll_t.
fxr_resolver.h is now a pure C header and fxr_resolver is C-only (no
fxr_resolver.cpp), which also drops it from the static singlefilehost
sources.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
nethost's own code is C, and after the previous change fxr_resolver is
C-only too, but nethost still linked the full hostmisc (which includes C++
translation units). That gave the nethost targets a C++ linker language, so
the clang++ driver added a dependency on the C++ runtime (libc++) and
libnethost.a embedded the unused C++ objects.
Split the hostmisc sources into C_SOURCES/CXX_SOURCES and add hostmisc_c, an
object library built from just the C sources. hostmisc and hostmisc::public
are unchanged for the C++ consumers (apphost, hostfxr, hostpolicy, etc.).
Point nethost/libnethost at hostmisc_c (+ minipal_objects, which object
libraries don't propagate transitively). The nethost targets now compile no
C++, link with the C driver, and have no libc++ dependency; libnethost.a
shrinks substantially by dropping the embedded C++ objects.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR completes the nethost conversion to C-only by rewriting nethost in C, moving remaining fxr_resolver functionality to the C surface, and adding a C PAL helper to detect an already-loaded hostfxr without introducing a C++ runtime dependency.

Changes:

  • Replaced nethost.cpp with nethost.c, switching to the C host APIs and manually saving/restoring the trace error writer.
  • Added C pal_get_loaded_library (and pal_dll_t) and routed the existing C++ pal::get_loaded_library through it.
  • Removed fxr_resolver.cpp and moved the delegate-loading template to load_fxr_and_get_delegate.h; updated C++ callers to use the C fxr_resolver_* APIs directly.
Show a summary per file
FileDescription
src/native/corehost/nethost/nethost.cppRemoved C++ implementation of get_hostfxr_path.
src/native/corehost/nethost/nethost.cNew C implementation of get_hostfxr_path using C host APIs.
src/native/corehost/nethost/CMakeLists.txtSwitched nethost sources to C and updated link deps to hostmisc_c.
src/native/corehost/load_fxr_and_get_delegate.hNew header containing the load_fxr_and_get_delegate C++ template previously in fxr_resolver.h.
src/native/corehost/ijwhost/ijwhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/comhost/comhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/hostmisc/pal.hAdded pal_dll_t and the C pal_get_loaded_library declaration.
src/native/corehost/hostmisc/pal.windows.cAdded pal_get_loaded_library implementation and refactored module-path helper.
src/native/corehost/hostmisc/pal.windows.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/pal.unix.cAdded pal_get_loaded_library implementation (dlopen RTLD_NOLOAD + dladdr, with /proc/self/maps fallback).
src/native/corehost/hostmisc/pal.unix.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/CMakeLists.txtSplit hostmisc C vs C++ sources and introduced hostmisc_c object library.
src/native/corehost/fxr_resolver.hRemoved C++ wrapper surface; added C fxr_resolver_try_get_existing_fxr.
src/native/corehost/fxr_resolver.cppRemoved thin C++ wrapper implementation.
src/native/corehost/fxr_resolver.cAdded C implementation of fxr_resolver_try_get_existing_fxr.
src/native/corehost/CMakeLists.txtUpdated fxr_resolver interface target to stop compiling fxr_resolver.cpp.
src/native/corehost/apphost/static/CMakeLists.txtDropped ../../fxr_resolver.cpp from sources.
src/native/corehost/apphost/standalone/hostfxr_resolver.cppSwitched from C++ wrappers to C fxr_resolver_try_get_path API.
src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.csUpdated comment to match renamed enum reference (fxr_search_location).

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 4

Comment threadsrc/native/corehost/nethost/nethost.c
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/fxr_resolver.c Outdated
Comment threadsrc/native/corehost/fxr_resolver.c
- nethost.c: use %zu (not %d) for the size_t in the invalid-size trace.
- pal.unix.c: bound the /proc/self/maps %s read with an explicit field
width so a pathological line can't overflow the buffer.
- fxr_resolver.c: initialize *out_fxr in fxr_resolver_try_get_existing_fxr,
and drop the stale fxr_resolver.cpp reference from the file comment.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
It only held a c_fx_ver_t, so pass the c_fx_ver_t directly as the
pal_readdir_onlydirectories context instead of wrapping it.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 25, 2026 20:26

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings June 25, 2026 20:38

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/nethost/CMakeLists.txt
Comment threadsrc/native/corehost/apphost/standalone/hostfxr_resolver.cpp Outdated
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
CopilotAI review requested due to automatic review settings June 30, 2026 15:27
In hostfxr_resolver_t's constructor, wrap the dotnet_root/fxr_path raw
pointers in a block so they go out of scope right after they are freed,
preventing later dangling-pointer use. The rest of the constructor only
needs the resolved flag and the assigned members.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 30, 2026 15:35

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 2

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/fxr_resolver.h Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings July 1, 2026 20:06

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
Co-authored-by: Elinor Fung <elfung@microsoft.com>
CopilotAI review requested due to automatic review settings July 1, 2026 22:48

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

@elinor-fung
elinor-fung merged commit ad0692a into dotnet:mainJul 2, 2026
173 checks passed
@elinor-fung
elinor-fung deleted the nethost-to-c branch July 2, 2026 17:13
@dotnet-milestone-botdotnet-milestone-botBot added this to the 11.0-preview7 milestone Jul 3, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
Convert `nethost` to C. This builds on #128420 and #129480 to fully
convert `nethost` so it compiles and links as C-only — no C++ runtime
dependency.
- **pal**
- Add a C `pal_get_loaded_library` (look up an already-loaded library
without loading it). The C++ `pal::get_loaded_library` now delegates to
it.
- **fxr_resolver** - Convert the last C++-only piece, `try_get_existing_fxr`, to C.
- Remove the thin C++ wrappers in `fxr_resolver.cpp`
- Move `load_fxr_and_get_delegate` (used by `comhost` and `ijwhost`)
into its own header `load_fxr_and_get_delegate.h`
- **nethost**
- Replace `nethost.cpp` with `nethost.c` using the shared host bits that
have been converted to C. The RAII `error_writer_scope_t` is replaced by
manual save/restore of the error writer.
- **Build**
- Split `hostmisc` into C/C++ sources and add a C-only `hostmisc_c`
object library
**linux x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---|---|---|---|
| `libnethost.a` | 2,743,470 | 532,650 | -2,210,820 | -80.6% |
| `libnethost.so` | 43,376 | 35,072 | -8,304 | -19.1% |
**macOS arm64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---:|---:|---:|---:|
| `libnethost.a` | 1,097,312 | 268,360 | −828,952 | −75.5% |
| `libnethost.dylib` | 92,368 | 89,088 | −3,280 | −3.6% |
**windows x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|--:|--:|--:|--:|
| `libnethost.lib` | 2,951,176 | 1,677,420 | −1,273,756 | −43.2% |
| `nethost.dll` | 71,168 | 56,832 | −14,336 | −20.1% |
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Aug 3, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

@elinor-fung@AaronRobinsonMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Convert nethost to C by elinor-fung · Pull Request #129828 · dotnet/runtime · GitHub
Skip to content

Convert nethost to C - #129828

Merged
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c
Jul 2, 2026
Merged

Convert nethost to C#129828
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c

Conversation

@elinor-fung

@elinor-fungelinor-fung commented Jun 25, 2026

Copy link
Copy Markdown
Member

Convert nethost to C. This builds on #128420 and #129480 to fully convert nethost so it compiles and links as C-only — no C++ runtime dependency.

cc @dotnet/appmodel @AaronRobinsonMSFT

  • pal
    • Add a C pal_get_loaded_library (look up an already-loaded library without loading it). The C++ pal::get_loaded_library now delegates to it.
  • fxr_resolver
    • Convert the last C++-only piece, try_get_existing_fxr, to C.
    • Remove the thin C++ wrappers in fxr_resolver.cpp
    • Move load_fxr_and_get_delegate (used by comhost and ijwhost) into its own header load_fxr_and_get_delegate.h
  • nethost
    • Replace nethost.cpp with nethost.c using the shared host bits that have been converted to C. The RAII error_writer_scope_t is replaced by manual save/restore of the error writer.
  • Build
    • Split hostmisc into C/C++ sources and add a C-only hostmisc_c object library

linux x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a2,743,470532,650-2,210,820-80.6%
libnethost.so43,37635,072-8,304-19.1%

macOS arm64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a1,097,312268,360−828,952−75.5%
libnethost.dylib92,36889,088−3,280−3.6%

windows x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.lib2,951,1761,677,420−1,273,756−43.2%
nethost.dll71,16856,832−14,336−20.1%

Note

This PR description was drafted by GitHub Copilot.

elinor-fungand others added 5 commits June 23, 2026 14:51
Add a C implementation of looking up an already-loaded library in the
current process, so callers that need to reuse an existing hostfxr (e.g.
nethost) can do so without the C++ pal:: surface.
- Add a pal_dll_t typedef and pal_get_loaded_library declaration in pal.h.
- Implement pal_get_loaded_library in pal.unix.c (dlopen RTLD_NOLOAD,
dladdr to resolve the path, and the /proc/self/maps fallback) and
pal.windows.c (GetModuleHandleW + a shared get_module_file_name helper
factored out of pal_get_own_executable_path).
- Repoint the C++ pal::get_loaded_library wrappers at the new C function,
removing the old C++ implementations and the proc-maps helper.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Now that pal_get_loaded_library is available in C, move the last
fxr_resolver function that was still C++-only into fxr_resolver.c.
Add fxr_resolver_try_get_existing_fxr (declaration in fxr_resolver.h,
implementation in fxr_resolver.c) and make the C++
fxr_resolver::try_get_existing_fxr wrapper delegate to it. Drop the file
comment noting that try_get_existing_fxr had to stay in C++.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Rewrite nethost.cpp as nethost.c using the C host APIs: trace_* for
tracing, fxr_resolver_try_get_existing_fxr / try_get_path_from_dotnet_root
/ try_get_path for resolution, and utils_get_directory. The RAII
error_writer_scope_t is replaced by saving and restoring the previous
error writer manually around the call, and pal::string_t buffers become
manually-managed pal_char_t* allocations.
Update nethost/CMakeLists.txt to build nethost.c. The compiled nethost
code is now C-only.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fxr_resolver.cpp held thin C++ wrappers (marshalling pal::string_t to and
from the pal_char_t* C API) over the fxr_resolver_* functions in
fxr_resolver.c. Remove them and have the remaining C++ callers use the C
API directly:
- hostfxr_resolver.cpp uses fxr_search_location and fxr_resolver_try_get_path
directly (with direct utils.h/error_codes.h includes that were previously
pulled in transitively through fxr_resolver.h).
- The load_fxr_and_get_delegate template (used by ijwhost and comhost) moves
to its own header, load_fxr_and_get_delegate.h, and calls the C resolver
functions. Its fxr handle is typed as pal_dll_t.
fxr_resolver.h is now a pure C header and fxr_resolver is C-only (no
fxr_resolver.cpp), which also drops it from the static singlefilehost
sources.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
nethost's own code is C, and after the previous change fxr_resolver is
C-only too, but nethost still linked the full hostmisc (which includes C++
translation units). That gave the nethost targets a C++ linker language, so
the clang++ driver added a dependency on the C++ runtime (libc++) and
libnethost.a embedded the unused C++ objects.
Split the hostmisc sources into C_SOURCES/CXX_SOURCES and add hostmisc_c, an
object library built from just the C sources. hostmisc and hostmisc::public
are unchanged for the C++ consumers (apphost, hostfxr, hostpolicy, etc.).
Point nethost/libnethost at hostmisc_c (+ minipal_objects, which object
libraries don't propagate transitively). The nethost targets now compile no
C++, link with the C driver, and have no libc++ dependency; libnethost.a
shrinks substantially by dropping the embedded C++ objects.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR completes the nethost conversion to C-only by rewriting nethost in C, moving remaining fxr_resolver functionality to the C surface, and adding a C PAL helper to detect an already-loaded hostfxr without introducing a C++ runtime dependency.

Changes:

  • Replaced nethost.cpp with nethost.c, switching to the C host APIs and manually saving/restoring the trace error writer.
  • Added C pal_get_loaded_library (and pal_dll_t) and routed the existing C++ pal::get_loaded_library through it.
  • Removed fxr_resolver.cpp and moved the delegate-loading template to load_fxr_and_get_delegate.h; updated C++ callers to use the C fxr_resolver_* APIs directly.
Show a summary per file
FileDescription
src/native/corehost/nethost/nethost.cppRemoved C++ implementation of get_hostfxr_path.
src/native/corehost/nethost/nethost.cNew C implementation of get_hostfxr_path using C host APIs.
src/native/corehost/nethost/CMakeLists.txtSwitched nethost sources to C and updated link deps to hostmisc_c.
src/native/corehost/load_fxr_and_get_delegate.hNew header containing the load_fxr_and_get_delegate C++ template previously in fxr_resolver.h.
src/native/corehost/ijwhost/ijwhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/comhost/comhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/hostmisc/pal.hAdded pal_dll_t and the C pal_get_loaded_library declaration.
src/native/corehost/hostmisc/pal.windows.cAdded pal_get_loaded_library implementation and refactored module-path helper.
src/native/corehost/hostmisc/pal.windows.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/pal.unix.cAdded pal_get_loaded_library implementation (dlopen RTLD_NOLOAD + dladdr, with /proc/self/maps fallback).
src/native/corehost/hostmisc/pal.unix.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/CMakeLists.txtSplit hostmisc C vs C++ sources and introduced hostmisc_c object library.
src/native/corehost/fxr_resolver.hRemoved C++ wrapper surface; added C fxr_resolver_try_get_existing_fxr.
src/native/corehost/fxr_resolver.cppRemoved thin C++ wrapper implementation.
src/native/corehost/fxr_resolver.cAdded C implementation of fxr_resolver_try_get_existing_fxr.
src/native/corehost/CMakeLists.txtUpdated fxr_resolver interface target to stop compiling fxr_resolver.cpp.
src/native/corehost/apphost/static/CMakeLists.txtDropped ../../fxr_resolver.cpp from sources.
src/native/corehost/apphost/standalone/hostfxr_resolver.cppSwitched from C++ wrappers to C fxr_resolver_try_get_path API.
src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.csUpdated comment to match renamed enum reference (fxr_search_location).

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 4

Comment threadsrc/native/corehost/nethost/nethost.c
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/fxr_resolver.c Outdated
Comment threadsrc/native/corehost/fxr_resolver.c
- nethost.c: use %zu (not %d) for the size_t in the invalid-size trace.
- pal.unix.c: bound the /proc/self/maps %s read with an explicit field
width so a pathological line can't overflow the buffer.
- fxr_resolver.c: initialize *out_fxr in fxr_resolver_try_get_existing_fxr,
and drop the stale fxr_resolver.cpp reference from the file comment.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
It only held a c_fx_ver_t, so pass the c_fx_ver_t directly as the
pal_readdir_onlydirectories context instead of wrapping it.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 25, 2026 20:26

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings June 25, 2026 20:38

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/nethost/CMakeLists.txt
Comment threadsrc/native/corehost/apphost/standalone/hostfxr_resolver.cpp Outdated
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
CopilotAI review requested due to automatic review settings June 30, 2026 15:27
In hostfxr_resolver_t's constructor, wrap the dotnet_root/fxr_path raw
pointers in a block so they go out of scope right after they are freed,
preventing later dangling-pointer use. The rest of the constructor only
needs the resolved flag and the assigned members.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 30, 2026 15:35

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 2

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/fxr_resolver.h Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings July 1, 2026 20:06

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
Co-authored-by: Elinor Fung <elfung@microsoft.com>
CopilotAI review requested due to automatic review settings July 1, 2026 22:48

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

@elinor-fung
elinor-fung merged commit ad0692a into dotnet:mainJul 2, 2026
173 checks passed
@elinor-fung
elinor-fung deleted the nethost-to-c branch July 2, 2026 17:13
@dotnet-milestone-botdotnet-milestone-botBot added this to the 11.0-preview7 milestone Jul 3, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
Convert `nethost` to C. This builds on #128420 and #129480 to fully
convert `nethost` so it compiles and links as C-only — no C++ runtime
dependency.
- **pal**
- Add a C `pal_get_loaded_library` (look up an already-loaded library
without loading it). The C++ `pal::get_loaded_library` now delegates to
it.
- **fxr_resolver** - Convert the last C++-only piece, `try_get_existing_fxr`, to C.
- Remove the thin C++ wrappers in `fxr_resolver.cpp`
- Move `load_fxr_and_get_delegate` (used by `comhost` and `ijwhost`)
into its own header `load_fxr_and_get_delegate.h`
- **nethost**
- Replace `nethost.cpp` with `nethost.c` using the shared host bits that
have been converted to C. The RAII `error_writer_scope_t` is replaced by
manual save/restore of the error writer.
- **Build**
- Split `hostmisc` into C/C++ sources and add a C-only `hostmisc_c`
object library
**linux x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---|---|---|---|
| `libnethost.a` | 2,743,470 | 532,650 | -2,210,820 | -80.6% |
| `libnethost.so` | 43,376 | 35,072 | -8,304 | -19.1% |
**macOS arm64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---:|---:|---:|---:|
| `libnethost.a` | 1,097,312 | 268,360 | −828,952 | −75.5% |
| `libnethost.dylib` | 92,368 | 89,088 | −3,280 | −3.6% |
**windows x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|--:|--:|--:|--:|
| `libnethost.lib` | 2,951,176 | 1,677,420 | −1,273,756 | −43.2% |
| `nethost.dll` | 71,168 | 56,832 | −14,336 | −20.1% |
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Aug 3, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

@elinor-fung@AaronRobinsonMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Convert nethost to C by elinor-fung · Pull Request #129828 · dotnet/runtime · GitHub
Skip to content

Convert nethost to C - #129828

Merged
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c
Jul 2, 2026
Merged

Convert nethost to C#129828
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c

Conversation

@elinor-fung

@elinor-fungelinor-fung commented Jun 25, 2026

Copy link
Copy Markdown
Member

Convert nethost to C. This builds on #128420 and #129480 to fully convert nethost so it compiles and links as C-only — no C++ runtime dependency.

cc @dotnet/appmodel @AaronRobinsonMSFT

  • pal
    • Add a C pal_get_loaded_library (look up an already-loaded library without loading it). The C++ pal::get_loaded_library now delegates to it.
  • fxr_resolver
    • Convert the last C++-only piece, try_get_existing_fxr, to C.
    • Remove the thin C++ wrappers in fxr_resolver.cpp
    • Move load_fxr_and_get_delegate (used by comhost and ijwhost) into its own header load_fxr_and_get_delegate.h
  • nethost
    • Replace nethost.cpp with nethost.c using the shared host bits that have been converted to C. The RAII error_writer_scope_t is replaced by manual save/restore of the error writer.
  • Build
    • Split hostmisc into C/C++ sources and add a C-only hostmisc_c object library

linux x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a2,743,470532,650-2,210,820-80.6%
libnethost.so43,37635,072-8,304-19.1%

macOS arm64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a1,097,312268,360−828,952−75.5%
libnethost.dylib92,36889,088−3,280−3.6%

windows x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.lib2,951,1761,677,420−1,273,756−43.2%
nethost.dll71,16856,832−14,336−20.1%

Note

This PR description was drafted by GitHub Copilot.

elinor-fungand others added 5 commits June 23, 2026 14:51
Add a C implementation of looking up an already-loaded library in the
current process, so callers that need to reuse an existing hostfxr (e.g.
nethost) can do so without the C++ pal:: surface.
- Add a pal_dll_t typedef and pal_get_loaded_library declaration in pal.h.
- Implement pal_get_loaded_library in pal.unix.c (dlopen RTLD_NOLOAD,
dladdr to resolve the path, and the /proc/self/maps fallback) and
pal.windows.c (GetModuleHandleW + a shared get_module_file_name helper
factored out of pal_get_own_executable_path).
- Repoint the C++ pal::get_loaded_library wrappers at the new C function,
removing the old C++ implementations and the proc-maps helper.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Now that pal_get_loaded_library is available in C, move the last
fxr_resolver function that was still C++-only into fxr_resolver.c.
Add fxr_resolver_try_get_existing_fxr (declaration in fxr_resolver.h,
implementation in fxr_resolver.c) and make the C++
fxr_resolver::try_get_existing_fxr wrapper delegate to it. Drop the file
comment noting that try_get_existing_fxr had to stay in C++.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Rewrite nethost.cpp as nethost.c using the C host APIs: trace_* for
tracing, fxr_resolver_try_get_existing_fxr / try_get_path_from_dotnet_root
/ try_get_path for resolution, and utils_get_directory. The RAII
error_writer_scope_t is replaced by saving and restoring the previous
error writer manually around the call, and pal::string_t buffers become
manually-managed pal_char_t* allocations.
Update nethost/CMakeLists.txt to build nethost.c. The compiled nethost
code is now C-only.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fxr_resolver.cpp held thin C++ wrappers (marshalling pal::string_t to and
from the pal_char_t* C API) over the fxr_resolver_* functions in
fxr_resolver.c. Remove them and have the remaining C++ callers use the C
API directly:
- hostfxr_resolver.cpp uses fxr_search_location and fxr_resolver_try_get_path
directly (with direct utils.h/error_codes.h includes that were previously
pulled in transitively through fxr_resolver.h).
- The load_fxr_and_get_delegate template (used by ijwhost and comhost) moves
to its own header, load_fxr_and_get_delegate.h, and calls the C resolver
functions. Its fxr handle is typed as pal_dll_t.
fxr_resolver.h is now a pure C header and fxr_resolver is C-only (no
fxr_resolver.cpp), which also drops it from the static singlefilehost
sources.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
nethost's own code is C, and after the previous change fxr_resolver is
C-only too, but nethost still linked the full hostmisc (which includes C++
translation units). That gave the nethost targets a C++ linker language, so
the clang++ driver added a dependency on the C++ runtime (libc++) and
libnethost.a embedded the unused C++ objects.
Split the hostmisc sources into C_SOURCES/CXX_SOURCES and add hostmisc_c, an
object library built from just the C sources. hostmisc and hostmisc::public
are unchanged for the C++ consumers (apphost, hostfxr, hostpolicy, etc.).
Point nethost/libnethost at hostmisc_c (+ minipal_objects, which object
libraries don't propagate transitively). The nethost targets now compile no
C++, link with the C driver, and have no libc++ dependency; libnethost.a
shrinks substantially by dropping the embedded C++ objects.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR completes the nethost conversion to C-only by rewriting nethost in C, moving remaining fxr_resolver functionality to the C surface, and adding a C PAL helper to detect an already-loaded hostfxr without introducing a C++ runtime dependency.

Changes:

  • Replaced nethost.cpp with nethost.c, switching to the C host APIs and manually saving/restoring the trace error writer.
  • Added C pal_get_loaded_library (and pal_dll_t) and routed the existing C++ pal::get_loaded_library through it.
  • Removed fxr_resolver.cpp and moved the delegate-loading template to load_fxr_and_get_delegate.h; updated C++ callers to use the C fxr_resolver_* APIs directly.
Show a summary per file
FileDescription
src/native/corehost/nethost/nethost.cppRemoved C++ implementation of get_hostfxr_path.
src/native/corehost/nethost/nethost.cNew C implementation of get_hostfxr_path using C host APIs.
src/native/corehost/nethost/CMakeLists.txtSwitched nethost sources to C and updated link deps to hostmisc_c.
src/native/corehost/load_fxr_and_get_delegate.hNew header containing the load_fxr_and_get_delegate C++ template previously in fxr_resolver.h.
src/native/corehost/ijwhost/ijwhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/comhost/comhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/hostmisc/pal.hAdded pal_dll_t and the C pal_get_loaded_library declaration.
src/native/corehost/hostmisc/pal.windows.cAdded pal_get_loaded_library implementation and refactored module-path helper.
src/native/corehost/hostmisc/pal.windows.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/pal.unix.cAdded pal_get_loaded_library implementation (dlopen RTLD_NOLOAD + dladdr, with /proc/self/maps fallback).
src/native/corehost/hostmisc/pal.unix.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/CMakeLists.txtSplit hostmisc C vs C++ sources and introduced hostmisc_c object library.
src/native/corehost/fxr_resolver.hRemoved C++ wrapper surface; added C fxr_resolver_try_get_existing_fxr.
src/native/corehost/fxr_resolver.cppRemoved thin C++ wrapper implementation.
src/native/corehost/fxr_resolver.cAdded C implementation of fxr_resolver_try_get_existing_fxr.
src/native/corehost/CMakeLists.txtUpdated fxr_resolver interface target to stop compiling fxr_resolver.cpp.
src/native/corehost/apphost/static/CMakeLists.txtDropped ../../fxr_resolver.cpp from sources.
src/native/corehost/apphost/standalone/hostfxr_resolver.cppSwitched from C++ wrappers to C fxr_resolver_try_get_path API.
src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.csUpdated comment to match renamed enum reference (fxr_search_location).

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 4

Comment threadsrc/native/corehost/nethost/nethost.c
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/fxr_resolver.c Outdated
Comment threadsrc/native/corehost/fxr_resolver.c
- nethost.c: use %zu (not %d) for the size_t in the invalid-size trace.
- pal.unix.c: bound the /proc/self/maps %s read with an explicit field
width so a pathological line can't overflow the buffer.
- fxr_resolver.c: initialize *out_fxr in fxr_resolver_try_get_existing_fxr,
and drop the stale fxr_resolver.cpp reference from the file comment.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
It only held a c_fx_ver_t, so pass the c_fx_ver_t directly as the
pal_readdir_onlydirectories context instead of wrapping it.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 25, 2026 20:26

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings June 25, 2026 20:38

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/nethost/CMakeLists.txt
Comment threadsrc/native/corehost/apphost/standalone/hostfxr_resolver.cpp Outdated
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
CopilotAI review requested due to automatic review settings June 30, 2026 15:27
In hostfxr_resolver_t's constructor, wrap the dotnet_root/fxr_path raw
pointers in a block so they go out of scope right after they are freed,
preventing later dangling-pointer use. The rest of the constructor only
needs the resolved flag and the assigned members.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 30, 2026 15:35

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 2

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/fxr_resolver.h Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings July 1, 2026 20:06

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
Co-authored-by: Elinor Fung <elfung@microsoft.com>
CopilotAI review requested due to automatic review settings July 1, 2026 22:48

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

@elinor-fung
elinor-fung merged commit ad0692a into dotnet:mainJul 2, 2026
173 checks passed
@elinor-fung
elinor-fung deleted the nethost-to-c branch July 2, 2026 17:13
@dotnet-milestone-botdotnet-milestone-botBot added this to the 11.0-preview7 milestone Jul 3, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
Convert `nethost` to C. This builds on #128420 and #129480 to fully
convert `nethost` so it compiles and links as C-only — no C++ runtime
dependency.
- **pal**
- Add a C `pal_get_loaded_library` (look up an already-loaded library
without loading it). The C++ `pal::get_loaded_library` now delegates to
it.
- **fxr_resolver** - Convert the last C++-only piece, `try_get_existing_fxr`, to C.
- Remove the thin C++ wrappers in `fxr_resolver.cpp`
- Move `load_fxr_and_get_delegate` (used by `comhost` and `ijwhost`)
into its own header `load_fxr_and_get_delegate.h`
- **nethost**
- Replace `nethost.cpp` with `nethost.c` using the shared host bits that
have been converted to C. The RAII `error_writer_scope_t` is replaced by
manual save/restore of the error writer.
- **Build**
- Split `hostmisc` into C/C++ sources and add a C-only `hostmisc_c`
object library
**linux x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---|---|---|---|
| `libnethost.a` | 2,743,470 | 532,650 | -2,210,820 | -80.6% |
| `libnethost.so` | 43,376 | 35,072 | -8,304 | -19.1% |
**macOS arm64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---:|---:|---:|---:|
| `libnethost.a` | 1,097,312 | 268,360 | −828,952 | −75.5% |
| `libnethost.dylib` | 92,368 | 89,088 | −3,280 | −3.6% |
**windows x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|--:|--:|--:|--:|
| `libnethost.lib` | 2,951,176 | 1,677,420 | −1,273,756 | −43.2% |
| `nethost.dll` | 71,168 | 56,832 | −14,336 | −20.1% |
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Aug 3, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

@elinor-fung@AaronRobinsonMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' Convert nethost to C by elinor-fung · Pull Request #129828 · dotnet/runtime · GitHub
Skip to content

Convert nethost to C - #129828

Merged
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c
Jul 2, 2026
Merged

Convert nethost to C#129828
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c

Conversation

@elinor-fung

@elinor-fungelinor-fung commented Jun 25, 2026

Copy link
Copy Markdown
Member

Convert nethost to C. This builds on #128420 and #129480 to fully convert nethost so it compiles and links as C-only — no C++ runtime dependency.

cc @dotnet/appmodel @AaronRobinsonMSFT

  • pal
    • Add a C pal_get_loaded_library (look up an already-loaded library without loading it). The C++ pal::get_loaded_library now delegates to it.
  • fxr_resolver
    • Convert the last C++-only piece, try_get_existing_fxr, to C.
    • Remove the thin C++ wrappers in fxr_resolver.cpp
    • Move load_fxr_and_get_delegate (used by comhost and ijwhost) into its own header load_fxr_and_get_delegate.h
  • nethost
    • Replace nethost.cpp with nethost.c using the shared host bits that have been converted to C. The RAII error_writer_scope_t is replaced by manual save/restore of the error writer.
  • Build
    • Split hostmisc into C/C++ sources and add a C-only hostmisc_c object library

linux x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a2,743,470532,650-2,210,820-80.6%
libnethost.so43,37635,072-8,304-19.1%

macOS arm64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a1,097,312268,360−828,952−75.5%
libnethost.dylib92,36889,088−3,280−3.6%

windows x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.lib2,951,1761,677,420−1,273,756−43.2%
nethost.dll71,16856,832−14,336−20.1%

Note

This PR description was drafted by GitHub Copilot.

elinor-fungand others added 5 commits June 23, 2026 14:51
Add a C implementation of looking up an already-loaded library in the
current process, so callers that need to reuse an existing hostfxr (e.g.
nethost) can do so without the C++ pal:: surface.
- Add a pal_dll_t typedef and pal_get_loaded_library declaration in pal.h.
- Implement pal_get_loaded_library in pal.unix.c (dlopen RTLD_NOLOAD,
dladdr to resolve the path, and the /proc/self/maps fallback) and
pal.windows.c (GetModuleHandleW + a shared get_module_file_name helper
factored out of pal_get_own_executable_path).
- Repoint the C++ pal::get_loaded_library wrappers at the new C function,
removing the old C++ implementations and the proc-maps helper.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Now that pal_get_loaded_library is available in C, move the last
fxr_resolver function that was still C++-only into fxr_resolver.c.
Add fxr_resolver_try_get_existing_fxr (declaration in fxr_resolver.h,
implementation in fxr_resolver.c) and make the C++
fxr_resolver::try_get_existing_fxr wrapper delegate to it. Drop the file
comment noting that try_get_existing_fxr had to stay in C++.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Rewrite nethost.cpp as nethost.c using the C host APIs: trace_* for
tracing, fxr_resolver_try_get_existing_fxr / try_get_path_from_dotnet_root
/ try_get_path for resolution, and utils_get_directory. The RAII
error_writer_scope_t is replaced by saving and restoring the previous
error writer manually around the call, and pal::string_t buffers become
manually-managed pal_char_t* allocations.
Update nethost/CMakeLists.txt to build nethost.c. The compiled nethost
code is now C-only.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fxr_resolver.cpp held thin C++ wrappers (marshalling pal::string_t to and
from the pal_char_t* C API) over the fxr_resolver_* functions in
fxr_resolver.c. Remove them and have the remaining C++ callers use the C
API directly:
- hostfxr_resolver.cpp uses fxr_search_location and fxr_resolver_try_get_path
directly (with direct utils.h/error_codes.h includes that were previously
pulled in transitively through fxr_resolver.h).
- The load_fxr_and_get_delegate template (used by ijwhost and comhost) moves
to its own header, load_fxr_and_get_delegate.h, and calls the C resolver
functions. Its fxr handle is typed as pal_dll_t.
fxr_resolver.h is now a pure C header and fxr_resolver is C-only (no
fxr_resolver.cpp), which also drops it from the static singlefilehost
sources.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
nethost's own code is C, and after the previous change fxr_resolver is
C-only too, but nethost still linked the full hostmisc (which includes C++
translation units). That gave the nethost targets a C++ linker language, so
the clang++ driver added a dependency on the C++ runtime (libc++) and
libnethost.a embedded the unused C++ objects.
Split the hostmisc sources into C_SOURCES/CXX_SOURCES and add hostmisc_c, an
object library built from just the C sources. hostmisc and hostmisc::public
are unchanged for the C++ consumers (apphost, hostfxr, hostpolicy, etc.).
Point nethost/libnethost at hostmisc_c (+ minipal_objects, which object
libraries don't propagate transitively). The nethost targets now compile no
C++, link with the C driver, and have no libc++ dependency; libnethost.a
shrinks substantially by dropping the embedded C++ objects.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR completes the nethost conversion to C-only by rewriting nethost in C, moving remaining fxr_resolver functionality to the C surface, and adding a C PAL helper to detect an already-loaded hostfxr without introducing a C++ runtime dependency.

Changes:

  • Replaced nethost.cpp with nethost.c, switching to the C host APIs and manually saving/restoring the trace error writer.
  • Added C pal_get_loaded_library (and pal_dll_t) and routed the existing C++ pal::get_loaded_library through it.
  • Removed fxr_resolver.cpp and moved the delegate-loading template to load_fxr_and_get_delegate.h; updated C++ callers to use the C fxr_resolver_* APIs directly.
Show a summary per file
FileDescription
src/native/corehost/nethost/nethost.cppRemoved C++ implementation of get_hostfxr_path.
src/native/corehost/nethost/nethost.cNew C implementation of get_hostfxr_path using C host APIs.
src/native/corehost/nethost/CMakeLists.txtSwitched nethost sources to C and updated link deps to hostmisc_c.
src/native/corehost/load_fxr_and_get_delegate.hNew header containing the load_fxr_and_get_delegate C++ template previously in fxr_resolver.h.
src/native/corehost/ijwhost/ijwhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/comhost/comhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/hostmisc/pal.hAdded pal_dll_t and the C pal_get_loaded_library declaration.
src/native/corehost/hostmisc/pal.windows.cAdded pal_get_loaded_library implementation and refactored module-path helper.
src/native/corehost/hostmisc/pal.windows.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/pal.unix.cAdded pal_get_loaded_library implementation (dlopen RTLD_NOLOAD + dladdr, with /proc/self/maps fallback).
src/native/corehost/hostmisc/pal.unix.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/CMakeLists.txtSplit hostmisc C vs C++ sources and introduced hostmisc_c object library.
src/native/corehost/fxr_resolver.hRemoved C++ wrapper surface; added C fxr_resolver_try_get_existing_fxr.
src/native/corehost/fxr_resolver.cppRemoved thin C++ wrapper implementation.
src/native/corehost/fxr_resolver.cAdded C implementation of fxr_resolver_try_get_existing_fxr.
src/native/corehost/CMakeLists.txtUpdated fxr_resolver interface target to stop compiling fxr_resolver.cpp.
src/native/corehost/apphost/static/CMakeLists.txtDropped ../../fxr_resolver.cpp from sources.
src/native/corehost/apphost/standalone/hostfxr_resolver.cppSwitched from C++ wrappers to C fxr_resolver_try_get_path API.
src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.csUpdated comment to match renamed enum reference (fxr_search_location).

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 4

Comment threadsrc/native/corehost/nethost/nethost.c
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/fxr_resolver.c Outdated
Comment threadsrc/native/corehost/fxr_resolver.c
- nethost.c: use %zu (not %d) for the size_t in the invalid-size trace.
- pal.unix.c: bound the /proc/self/maps %s read with an explicit field
width so a pathological line can't overflow the buffer.
- fxr_resolver.c: initialize *out_fxr in fxr_resolver_try_get_existing_fxr,
and drop the stale fxr_resolver.cpp reference from the file comment.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
It only held a c_fx_ver_t, so pass the c_fx_ver_t directly as the
pal_readdir_onlydirectories context instead of wrapping it.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 25, 2026 20:26

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings June 25, 2026 20:38

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/nethost/CMakeLists.txt
Comment threadsrc/native/corehost/apphost/standalone/hostfxr_resolver.cpp Outdated
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
CopilotAI review requested due to automatic review settings June 30, 2026 15:27
In hostfxr_resolver_t's constructor, wrap the dotnet_root/fxr_path raw
pointers in a block so they go out of scope right after they are freed,
preventing later dangling-pointer use. The rest of the constructor only
needs the resolved flag and the assigned members.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 30, 2026 15:35

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 2

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/fxr_resolver.h Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings July 1, 2026 20:06

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
Co-authored-by: Elinor Fung <elfung@microsoft.com>
CopilotAI review requested due to automatic review settings July 1, 2026 22:48

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

@elinor-fung
elinor-fung merged commit ad0692a into dotnet:mainJul 2, 2026
173 checks passed
@elinor-fung
elinor-fung deleted the nethost-to-c branch July 2, 2026 17:13
@dotnet-milestone-botdotnet-milestone-botBot added this to the 11.0-preview7 milestone Jul 3, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
Convert `nethost` to C. This builds on #128420 and #129480 to fully
convert `nethost` so it compiles and links as C-only — no C++ runtime
dependency.
- **pal**
- Add a C `pal_get_loaded_library` (look up an already-loaded library
without loading it). The C++ `pal::get_loaded_library` now delegates to
it.
- **fxr_resolver** - Convert the last C++-only piece, `try_get_existing_fxr`, to C.
- Remove the thin C++ wrappers in `fxr_resolver.cpp`
- Move `load_fxr_and_get_delegate` (used by `comhost` and `ijwhost`)
into its own header `load_fxr_and_get_delegate.h`
- **nethost**
- Replace `nethost.cpp` with `nethost.c` using the shared host bits that
have been converted to C. The RAII `error_writer_scope_t` is replaced by
manual save/restore of the error writer.
- **Build**
- Split `hostmisc` into C/C++ sources and add a C-only `hostmisc_c`
object library
**linux x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---|---|---|---|
| `libnethost.a` | 2,743,470 | 532,650 | -2,210,820 | -80.6% |
| `libnethost.so` | 43,376 | 35,072 | -8,304 | -19.1% |
**macOS arm64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---:|---:|---:|---:|
| `libnethost.a` | 1,097,312 | 268,360 | −828,952 | −75.5% |
| `libnethost.dylib` | 92,368 | 89,088 | −3,280 | −3.6% |
**windows x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|--:|--:|--:|--:|
| `libnethost.lib` | 2,951,176 | 1,677,420 | −1,273,756 | −43.2% |
| `nethost.dll` | 71,168 | 56,832 | −14,336 | −20.1% |
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Aug 3, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

@elinor-fung@AaronRobinsonMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Convert nethost to C by elinor-fung · Pull Request #129828 · dotnet/runtime · GitHub
Skip to content

Convert nethost to C - #129828

Merged
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c
Jul 2, 2026
Merged

Convert nethost to C#129828
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c

Conversation

@elinor-fung

@elinor-fungelinor-fung commented Jun 25, 2026

Copy link
Copy Markdown
Member

Convert nethost to C. This builds on #128420 and #129480 to fully convert nethost so it compiles and links as C-only — no C++ runtime dependency.

cc @dotnet/appmodel @AaronRobinsonMSFT

  • pal
    • Add a C pal_get_loaded_library (look up an already-loaded library without loading it). The C++ pal::get_loaded_library now delegates to it.
  • fxr_resolver
    • Convert the last C++-only piece, try_get_existing_fxr, to C.
    • Remove the thin C++ wrappers in fxr_resolver.cpp
    • Move load_fxr_and_get_delegate (used by comhost and ijwhost) into its own header load_fxr_and_get_delegate.h
  • nethost
    • Replace nethost.cpp with nethost.c using the shared host bits that have been converted to C. The RAII error_writer_scope_t is replaced by manual save/restore of the error writer.
  • Build
    • Split hostmisc into C/C++ sources and add a C-only hostmisc_c object library

linux x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a2,743,470532,650-2,210,820-80.6%
libnethost.so43,37635,072-8,304-19.1%

macOS arm64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a1,097,312268,360−828,952−75.5%
libnethost.dylib92,36889,088−3,280−3.6%

windows x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.lib2,951,1761,677,420−1,273,756−43.2%
nethost.dll71,16856,832−14,336−20.1%

Note

This PR description was drafted by GitHub Copilot.

elinor-fungand others added 5 commits June 23, 2026 14:51
Add a C implementation of looking up an already-loaded library in the
current process, so callers that need to reuse an existing hostfxr (e.g.
nethost) can do so without the C++ pal:: surface.
- Add a pal_dll_t typedef and pal_get_loaded_library declaration in pal.h.
- Implement pal_get_loaded_library in pal.unix.c (dlopen RTLD_NOLOAD,
dladdr to resolve the path, and the /proc/self/maps fallback) and
pal.windows.c (GetModuleHandleW + a shared get_module_file_name helper
factored out of pal_get_own_executable_path).
- Repoint the C++ pal::get_loaded_library wrappers at the new C function,
removing the old C++ implementations and the proc-maps helper.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Now that pal_get_loaded_library is available in C, move the last
fxr_resolver function that was still C++-only into fxr_resolver.c.
Add fxr_resolver_try_get_existing_fxr (declaration in fxr_resolver.h,
implementation in fxr_resolver.c) and make the C++
fxr_resolver::try_get_existing_fxr wrapper delegate to it. Drop the file
comment noting that try_get_existing_fxr had to stay in C++.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Rewrite nethost.cpp as nethost.c using the C host APIs: trace_* for
tracing, fxr_resolver_try_get_existing_fxr / try_get_path_from_dotnet_root
/ try_get_path for resolution, and utils_get_directory. The RAII
error_writer_scope_t is replaced by saving and restoring the previous
error writer manually around the call, and pal::string_t buffers become
manually-managed pal_char_t* allocations.
Update nethost/CMakeLists.txt to build nethost.c. The compiled nethost
code is now C-only.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fxr_resolver.cpp held thin C++ wrappers (marshalling pal::string_t to and
from the pal_char_t* C API) over the fxr_resolver_* functions in
fxr_resolver.c. Remove them and have the remaining C++ callers use the C
API directly:
- hostfxr_resolver.cpp uses fxr_search_location and fxr_resolver_try_get_path
directly (with direct utils.h/error_codes.h includes that were previously
pulled in transitively through fxr_resolver.h).
- The load_fxr_and_get_delegate template (used by ijwhost and comhost) moves
to its own header, load_fxr_and_get_delegate.h, and calls the C resolver
functions. Its fxr handle is typed as pal_dll_t.
fxr_resolver.h is now a pure C header and fxr_resolver is C-only (no
fxr_resolver.cpp), which also drops it from the static singlefilehost
sources.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
nethost's own code is C, and after the previous change fxr_resolver is
C-only too, but nethost still linked the full hostmisc (which includes C++
translation units). That gave the nethost targets a C++ linker language, so
the clang++ driver added a dependency on the C++ runtime (libc++) and
libnethost.a embedded the unused C++ objects.
Split the hostmisc sources into C_SOURCES/CXX_SOURCES and add hostmisc_c, an
object library built from just the C sources. hostmisc and hostmisc::public
are unchanged for the C++ consumers (apphost, hostfxr, hostpolicy, etc.).
Point nethost/libnethost at hostmisc_c (+ minipal_objects, which object
libraries don't propagate transitively). The nethost targets now compile no
C++, link with the C driver, and have no libc++ dependency; libnethost.a
shrinks substantially by dropping the embedded C++ objects.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR completes the nethost conversion to C-only by rewriting nethost in C, moving remaining fxr_resolver functionality to the C surface, and adding a C PAL helper to detect an already-loaded hostfxr without introducing a C++ runtime dependency.

Changes:

  • Replaced nethost.cpp with nethost.c, switching to the C host APIs and manually saving/restoring the trace error writer.
  • Added C pal_get_loaded_library (and pal_dll_t) and routed the existing C++ pal::get_loaded_library through it.
  • Removed fxr_resolver.cpp and moved the delegate-loading template to load_fxr_and_get_delegate.h; updated C++ callers to use the C fxr_resolver_* APIs directly.
Show a summary per file
FileDescription
src/native/corehost/nethost/nethost.cppRemoved C++ implementation of get_hostfxr_path.
src/native/corehost/nethost/nethost.cNew C implementation of get_hostfxr_path using C host APIs.
src/native/corehost/nethost/CMakeLists.txtSwitched nethost sources to C and updated link deps to hostmisc_c.
src/native/corehost/load_fxr_and_get_delegate.hNew header containing the load_fxr_and_get_delegate C++ template previously in fxr_resolver.h.
src/native/corehost/ijwhost/ijwhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/comhost/comhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/hostmisc/pal.hAdded pal_dll_t and the C pal_get_loaded_library declaration.
src/native/corehost/hostmisc/pal.windows.cAdded pal_get_loaded_library implementation and refactored module-path helper.
src/native/corehost/hostmisc/pal.windows.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/pal.unix.cAdded pal_get_loaded_library implementation (dlopen RTLD_NOLOAD + dladdr, with /proc/self/maps fallback).
src/native/corehost/hostmisc/pal.unix.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/CMakeLists.txtSplit hostmisc C vs C++ sources and introduced hostmisc_c object library.
src/native/corehost/fxr_resolver.hRemoved C++ wrapper surface; added C fxr_resolver_try_get_existing_fxr.
src/native/corehost/fxr_resolver.cppRemoved thin C++ wrapper implementation.
src/native/corehost/fxr_resolver.cAdded C implementation of fxr_resolver_try_get_existing_fxr.
src/native/corehost/CMakeLists.txtUpdated fxr_resolver interface target to stop compiling fxr_resolver.cpp.
src/native/corehost/apphost/static/CMakeLists.txtDropped ../../fxr_resolver.cpp from sources.
src/native/corehost/apphost/standalone/hostfxr_resolver.cppSwitched from C++ wrappers to C fxr_resolver_try_get_path API.
src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.csUpdated comment to match renamed enum reference (fxr_search_location).

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 4

Comment threadsrc/native/corehost/nethost/nethost.c
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/fxr_resolver.c Outdated
Comment threadsrc/native/corehost/fxr_resolver.c
- nethost.c: use %zu (not %d) for the size_t in the invalid-size trace.
- pal.unix.c: bound the /proc/self/maps %s read with an explicit field
width so a pathological line can't overflow the buffer.
- fxr_resolver.c: initialize *out_fxr in fxr_resolver_try_get_existing_fxr,
and drop the stale fxr_resolver.cpp reference from the file comment.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
It only held a c_fx_ver_t, so pass the c_fx_ver_t directly as the
pal_readdir_onlydirectories context instead of wrapping it.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 25, 2026 20:26

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings June 25, 2026 20:38

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/nethost/CMakeLists.txt
Comment threadsrc/native/corehost/apphost/standalone/hostfxr_resolver.cpp Outdated
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
CopilotAI review requested due to automatic review settings June 30, 2026 15:27
In hostfxr_resolver_t's constructor, wrap the dotnet_root/fxr_path raw
pointers in a block so they go out of scope right after they are freed,
preventing later dangling-pointer use. The rest of the constructor only
needs the resolved flag and the assigned members.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 30, 2026 15:35

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 2

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/fxr_resolver.h Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings July 1, 2026 20:06

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
Co-authored-by: Elinor Fung <elfung@microsoft.com>
CopilotAI review requested due to automatic review settings July 1, 2026 22:48

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

@elinor-fung
elinor-fung merged commit ad0692a into dotnet:mainJul 2, 2026
173 checks passed
@elinor-fung
elinor-fung deleted the nethost-to-c branch July 2, 2026 17:13
@dotnet-milestone-botdotnet-milestone-botBot added this to the 11.0-preview7 milestone Jul 3, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
Convert `nethost` to C. This builds on #128420 and #129480 to fully
convert `nethost` so it compiles and links as C-only — no C++ runtime
dependency.
- **pal**
- Add a C `pal_get_loaded_library` (look up an already-loaded library
without loading it). The C++ `pal::get_loaded_library` now delegates to
it.
- **fxr_resolver** - Convert the last C++-only piece, `try_get_existing_fxr`, to C.
- Remove the thin C++ wrappers in `fxr_resolver.cpp`
- Move `load_fxr_and_get_delegate` (used by `comhost` and `ijwhost`)
into its own header `load_fxr_and_get_delegate.h`
- **nethost**
- Replace `nethost.cpp` with `nethost.c` using the shared host bits that
have been converted to C. The RAII `error_writer_scope_t` is replaced by
manual save/restore of the error writer.
- **Build**
- Split `hostmisc` into C/C++ sources and add a C-only `hostmisc_c`
object library
**linux x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---|---|---|---|
| `libnethost.a` | 2,743,470 | 532,650 | -2,210,820 | -80.6% |
| `libnethost.so` | 43,376 | 35,072 | -8,304 | -19.1% |
**macOS arm64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---:|---:|---:|---:|
| `libnethost.a` | 1,097,312 | 268,360 | −828,952 | −75.5% |
| `libnethost.dylib` | 92,368 | 89,088 | −3,280 | −3.6% |
**windows x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|--:|--:|--:|--:|
| `libnethost.lib` | 2,951,176 | 1,677,420 | −1,273,756 | −43.2% |
| `nethost.dll` | 71,168 | 56,832 | −14,336 | −20.1% |
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Aug 3, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

@elinor-fung@AaronRobinsonMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Convert nethost to C by elinor-fung · Pull Request #129828 · dotnet/runtime · GitHub
Skip to content

Convert nethost to C - #129828

Merged
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c
Jul 2, 2026
Merged

Convert nethost to C#129828
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c

Conversation

@elinor-fung

@elinor-fungelinor-fung commented Jun 25, 2026

Copy link
Copy Markdown
Member

Convert nethost to C. This builds on #128420 and #129480 to fully convert nethost so it compiles and links as C-only — no C++ runtime dependency.

cc @dotnet/appmodel @AaronRobinsonMSFT

  • pal
    • Add a C pal_get_loaded_library (look up an already-loaded library without loading it). The C++ pal::get_loaded_library now delegates to it.
  • fxr_resolver
    • Convert the last C++-only piece, try_get_existing_fxr, to C.
    • Remove the thin C++ wrappers in fxr_resolver.cpp
    • Move load_fxr_and_get_delegate (used by comhost and ijwhost) into its own header load_fxr_and_get_delegate.h
  • nethost
    • Replace nethost.cpp with nethost.c using the shared host bits that have been converted to C. The RAII error_writer_scope_t is replaced by manual save/restore of the error writer.
  • Build
    • Split hostmisc into C/C++ sources and add a C-only hostmisc_c object library

linux x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a2,743,470532,650-2,210,820-80.6%
libnethost.so43,37635,072-8,304-19.1%

macOS arm64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a1,097,312268,360−828,952−75.5%
libnethost.dylib92,36889,088−3,280−3.6%

windows x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.lib2,951,1761,677,420−1,273,756−43.2%
nethost.dll71,16856,832−14,336−20.1%

Note

This PR description was drafted by GitHub Copilot.

elinor-fungand others added 5 commits June 23, 2026 14:51
Add a C implementation of looking up an already-loaded library in the
current process, so callers that need to reuse an existing hostfxr (e.g.
nethost) can do so without the C++ pal:: surface.
- Add a pal_dll_t typedef and pal_get_loaded_library declaration in pal.h.
- Implement pal_get_loaded_library in pal.unix.c (dlopen RTLD_NOLOAD,
dladdr to resolve the path, and the /proc/self/maps fallback) and
pal.windows.c (GetModuleHandleW + a shared get_module_file_name helper
factored out of pal_get_own_executable_path).
- Repoint the C++ pal::get_loaded_library wrappers at the new C function,
removing the old C++ implementations and the proc-maps helper.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Now that pal_get_loaded_library is available in C, move the last
fxr_resolver function that was still C++-only into fxr_resolver.c.
Add fxr_resolver_try_get_existing_fxr (declaration in fxr_resolver.h,
implementation in fxr_resolver.c) and make the C++
fxr_resolver::try_get_existing_fxr wrapper delegate to it. Drop the file
comment noting that try_get_existing_fxr had to stay in C++.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Rewrite nethost.cpp as nethost.c using the C host APIs: trace_* for
tracing, fxr_resolver_try_get_existing_fxr / try_get_path_from_dotnet_root
/ try_get_path for resolution, and utils_get_directory. The RAII
error_writer_scope_t is replaced by saving and restoring the previous
error writer manually around the call, and pal::string_t buffers become
manually-managed pal_char_t* allocations.
Update nethost/CMakeLists.txt to build nethost.c. The compiled nethost
code is now C-only.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fxr_resolver.cpp held thin C++ wrappers (marshalling pal::string_t to and
from the pal_char_t* C API) over the fxr_resolver_* functions in
fxr_resolver.c. Remove them and have the remaining C++ callers use the C
API directly:
- hostfxr_resolver.cpp uses fxr_search_location and fxr_resolver_try_get_path
directly (with direct utils.h/error_codes.h includes that were previously
pulled in transitively through fxr_resolver.h).
- The load_fxr_and_get_delegate template (used by ijwhost and comhost) moves
to its own header, load_fxr_and_get_delegate.h, and calls the C resolver
functions. Its fxr handle is typed as pal_dll_t.
fxr_resolver.h is now a pure C header and fxr_resolver is C-only (no
fxr_resolver.cpp), which also drops it from the static singlefilehost
sources.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
nethost's own code is C, and after the previous change fxr_resolver is
C-only too, but nethost still linked the full hostmisc (which includes C++
translation units). That gave the nethost targets a C++ linker language, so
the clang++ driver added a dependency on the C++ runtime (libc++) and
libnethost.a embedded the unused C++ objects.
Split the hostmisc sources into C_SOURCES/CXX_SOURCES and add hostmisc_c, an
object library built from just the C sources. hostmisc and hostmisc::public
are unchanged for the C++ consumers (apphost, hostfxr, hostpolicy, etc.).
Point nethost/libnethost at hostmisc_c (+ minipal_objects, which object
libraries don't propagate transitively). The nethost targets now compile no
C++, link with the C driver, and have no libc++ dependency; libnethost.a
shrinks substantially by dropping the embedded C++ objects.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR completes the nethost conversion to C-only by rewriting nethost in C, moving remaining fxr_resolver functionality to the C surface, and adding a C PAL helper to detect an already-loaded hostfxr without introducing a C++ runtime dependency.

Changes:

  • Replaced nethost.cpp with nethost.c, switching to the C host APIs and manually saving/restoring the trace error writer.
  • Added C pal_get_loaded_library (and pal_dll_t) and routed the existing C++ pal::get_loaded_library through it.
  • Removed fxr_resolver.cpp and moved the delegate-loading template to load_fxr_and_get_delegate.h; updated C++ callers to use the C fxr_resolver_* APIs directly.
Show a summary per file
FileDescription
src/native/corehost/nethost/nethost.cppRemoved C++ implementation of get_hostfxr_path.
src/native/corehost/nethost/nethost.cNew C implementation of get_hostfxr_path using C host APIs.
src/native/corehost/nethost/CMakeLists.txtSwitched nethost sources to C and updated link deps to hostmisc_c.
src/native/corehost/load_fxr_and_get_delegate.hNew header containing the load_fxr_and_get_delegate C++ template previously in fxr_resolver.h.
src/native/corehost/ijwhost/ijwhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/comhost/comhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/hostmisc/pal.hAdded pal_dll_t and the C pal_get_loaded_library declaration.
src/native/corehost/hostmisc/pal.windows.cAdded pal_get_loaded_library implementation and refactored module-path helper.
src/native/corehost/hostmisc/pal.windows.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/pal.unix.cAdded pal_get_loaded_library implementation (dlopen RTLD_NOLOAD + dladdr, with /proc/self/maps fallback).
src/native/corehost/hostmisc/pal.unix.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/CMakeLists.txtSplit hostmisc C vs C++ sources and introduced hostmisc_c object library.
src/native/corehost/fxr_resolver.hRemoved C++ wrapper surface; added C fxr_resolver_try_get_existing_fxr.
src/native/corehost/fxr_resolver.cppRemoved thin C++ wrapper implementation.
src/native/corehost/fxr_resolver.cAdded C implementation of fxr_resolver_try_get_existing_fxr.
src/native/corehost/CMakeLists.txtUpdated fxr_resolver interface target to stop compiling fxr_resolver.cpp.
src/native/corehost/apphost/static/CMakeLists.txtDropped ../../fxr_resolver.cpp from sources.
src/native/corehost/apphost/standalone/hostfxr_resolver.cppSwitched from C++ wrappers to C fxr_resolver_try_get_path API.
src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.csUpdated comment to match renamed enum reference (fxr_search_location).

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 4

Comment threadsrc/native/corehost/nethost/nethost.c
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/fxr_resolver.c Outdated
Comment threadsrc/native/corehost/fxr_resolver.c
- nethost.c: use %zu (not %d) for the size_t in the invalid-size trace.
- pal.unix.c: bound the /proc/self/maps %s read with an explicit field
width so a pathological line can't overflow the buffer.
- fxr_resolver.c: initialize *out_fxr in fxr_resolver_try_get_existing_fxr,
and drop the stale fxr_resolver.cpp reference from the file comment.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
It only held a c_fx_ver_t, so pass the c_fx_ver_t directly as the
pal_readdir_onlydirectories context instead of wrapping it.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 25, 2026 20:26

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings June 25, 2026 20:38

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/nethost/CMakeLists.txt
Comment threadsrc/native/corehost/apphost/standalone/hostfxr_resolver.cpp Outdated
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
CopilotAI review requested due to automatic review settings June 30, 2026 15:27
In hostfxr_resolver_t's constructor, wrap the dotnet_root/fxr_path raw
pointers in a block so they go out of scope right after they are freed,
preventing later dangling-pointer use. The rest of the constructor only
needs the resolved flag and the assigned members.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 30, 2026 15:35

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 2

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/fxr_resolver.h Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings July 1, 2026 20:06

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
Co-authored-by: Elinor Fung <elfung@microsoft.com>
CopilotAI review requested due to automatic review settings July 1, 2026 22:48

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

@elinor-fung
elinor-fung merged commit ad0692a into dotnet:mainJul 2, 2026
173 checks passed
@elinor-fung
elinor-fung deleted the nethost-to-c branch July 2, 2026 17:13
@dotnet-milestone-botdotnet-milestone-botBot added this to the 11.0-preview7 milestone Jul 3, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
Convert `nethost` to C. This builds on #128420 and #129480 to fully
convert `nethost` so it compiles and links as C-only — no C++ runtime
dependency.
- **pal**
- Add a C `pal_get_loaded_library` (look up an already-loaded library
without loading it). The C++ `pal::get_loaded_library` now delegates to
it.
- **fxr_resolver** - Convert the last C++-only piece, `try_get_existing_fxr`, to C.
- Remove the thin C++ wrappers in `fxr_resolver.cpp`
- Move `load_fxr_and_get_delegate` (used by `comhost` and `ijwhost`)
into its own header `load_fxr_and_get_delegate.h`
- **nethost**
- Replace `nethost.cpp` with `nethost.c` using the shared host bits that
have been converted to C. The RAII `error_writer_scope_t` is replaced by
manual save/restore of the error writer.
- **Build**
- Split `hostmisc` into C/C++ sources and add a C-only `hostmisc_c`
object library
**linux x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---|---|---|---|
| `libnethost.a` | 2,743,470 | 532,650 | -2,210,820 | -80.6% |
| `libnethost.so` | 43,376 | 35,072 | -8,304 | -19.1% |
**macOS arm64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---:|---:|---:|---:|
| `libnethost.a` | 1,097,312 | 268,360 | −828,952 | −75.5% |
| `libnethost.dylib` | 92,368 | 89,088 | −3,280 | −3.6% |
**windows x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|--:|--:|--:|--:|
| `libnethost.lib` | 2,951,176 | 1,677,420 | −1,273,756 | −43.2% |
| `nethost.dll` | 71,168 | 56,832 | −14,336 | −20.1% |
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Aug 3, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

@elinor-fung@AaronRobinsonMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); Convert nethost to C by elinor-fung · Pull Request #129828 · dotnet/runtime · GitHub
Skip to content

Convert nethost to C - #129828

Merged
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c
Jul 2, 2026
Merged

Convert nethost to C#129828
elinor-fung merged 12 commits into
dotnet:mainfrom
elinor-fung:nethost-to-c

Conversation

@elinor-fung

@elinor-fungelinor-fung commented Jun 25, 2026

Copy link
Copy Markdown
Member

Convert nethost to C. This builds on #128420 and #129480 to fully convert nethost so it compiles and links as C-only — no C++ runtime dependency.

cc @dotnet/appmodel @AaronRobinsonMSFT

  • pal
    • Add a C pal_get_loaded_library (look up an already-loaded library without loading it). The C++ pal::get_loaded_library now delegates to it.
  • fxr_resolver
    • Convert the last C++-only piece, try_get_existing_fxr, to C.
    • Remove the thin C++ wrappers in fxr_resolver.cpp
    • Move load_fxr_and_get_delegate (used by comhost and ijwhost) into its own header load_fxr_and_get_delegate.h
  • nethost
    • Replace nethost.cpp with nethost.c using the shared host bits that have been converted to C. The RAII error_writer_scope_t is replaced by manual save/restore of the error writer.
  • Build
    • Split hostmisc into C/C++ sources and add a C-only hostmisc_c object library

linux x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a2,743,470532,650-2,210,820-80.6%
libnethost.so43,37635,072-8,304-19.1%

macOS arm64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.a1,097,312268,360−828,952−75.5%
libnethost.dylib92,36889,088−3,280−3.6%

windows x64 Release

BinaryBaselinePRΔ bytesΔ %
libnethost.lib2,951,1761,677,420−1,273,756−43.2%
nethost.dll71,16856,832−14,336−20.1%

Note

This PR description was drafted by GitHub Copilot.

elinor-fungand others added 5 commits June 23, 2026 14:51
Add a C implementation of looking up an already-loaded library in the
current process, so callers that need to reuse an existing hostfxr (e.g.
nethost) can do so without the C++ pal:: surface.
- Add a pal_dll_t typedef and pal_get_loaded_library declaration in pal.h.
- Implement pal_get_loaded_library in pal.unix.c (dlopen RTLD_NOLOAD,
dladdr to resolve the path, and the /proc/self/maps fallback) and
pal.windows.c (GetModuleHandleW + a shared get_module_file_name helper
factored out of pal_get_own_executable_path).
- Repoint the C++ pal::get_loaded_library wrappers at the new C function,
removing the old C++ implementations and the proc-maps helper.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Now that pal_get_loaded_library is available in C, move the last
fxr_resolver function that was still C++-only into fxr_resolver.c.
Add fxr_resolver_try_get_existing_fxr (declaration in fxr_resolver.h,
implementation in fxr_resolver.c) and make the C++
fxr_resolver::try_get_existing_fxr wrapper delegate to it. Drop the file
comment noting that try_get_existing_fxr had to stay in C++.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Rewrite nethost.cpp as nethost.c using the C host APIs: trace_* for
tracing, fxr_resolver_try_get_existing_fxr / try_get_path_from_dotnet_root
/ try_get_path for resolution, and utils_get_directory. The RAII
error_writer_scope_t is replaced by saving and restoring the previous
error writer manually around the call, and pal::string_t buffers become
manually-managed pal_char_t* allocations.
Update nethost/CMakeLists.txt to build nethost.c. The compiled nethost
code is now C-only.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fxr_resolver.cpp held thin C++ wrappers (marshalling pal::string_t to and
from the pal_char_t* C API) over the fxr_resolver_* functions in
fxr_resolver.c. Remove them and have the remaining C++ callers use the C
API directly:
- hostfxr_resolver.cpp uses fxr_search_location and fxr_resolver_try_get_path
directly (with direct utils.h/error_codes.h includes that were previously
pulled in transitively through fxr_resolver.h).
- The load_fxr_and_get_delegate template (used by ijwhost and comhost) moves
to its own header, load_fxr_and_get_delegate.h, and calls the C resolver
functions. Its fxr handle is typed as pal_dll_t.
fxr_resolver.h is now a pure C header and fxr_resolver is C-only (no
fxr_resolver.cpp), which also drops it from the static singlefilehost
sources.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
nethost's own code is C, and after the previous change fxr_resolver is
C-only too, but nethost still linked the full hostmisc (which includes C++
translation units). That gave the nethost targets a C++ linker language, so
the clang++ driver added a dependency on the C++ runtime (libc++) and
libnethost.a embedded the unused C++ objects.
Split the hostmisc sources into C_SOURCES/CXX_SOURCES and add hostmisc_c, an
object library built from just the C sources. hostmisc and hostmisc::public
are unchanged for the C++ consumers (apphost, hostfxr, hostpolicy, etc.).
Point nethost/libnethost at hostmisc_c (+ minipal_objects, which object
libraries don't propagate transitively). The nethost targets now compile no
C++, link with the C driver, and have no libc++ dependency; libnethost.a
shrinks substantially by dropping the embedded C++ objects.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR completes the nethost conversion to C-only by rewriting nethost in C, moving remaining fxr_resolver functionality to the C surface, and adding a C PAL helper to detect an already-loaded hostfxr without introducing a C++ runtime dependency.

Changes:

  • Replaced nethost.cpp with nethost.c, switching to the C host APIs and manually saving/restoring the trace error writer.
  • Added C pal_get_loaded_library (and pal_dll_t) and routed the existing C++ pal::get_loaded_library through it.
  • Removed fxr_resolver.cpp and moved the delegate-loading template to load_fxr_and_get_delegate.h; updated C++ callers to use the C fxr_resolver_* APIs directly.
Show a summary per file
FileDescription
src/native/corehost/nethost/nethost.cppRemoved C++ implementation of get_hostfxr_path.
src/native/corehost/nethost/nethost.cNew C implementation of get_hostfxr_path using C host APIs.
src/native/corehost/nethost/CMakeLists.txtSwitched nethost sources to C and updated link deps to hostmisc_c.
src/native/corehost/load_fxr_and_get_delegate.hNew header containing the load_fxr_and_get_delegate C++ template previously in fxr_resolver.h.
src/native/corehost/ijwhost/ijwhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/comhost/comhost.cppUpdated include to use load_fxr_and_get_delegate.h.
src/native/corehost/hostmisc/pal.hAdded pal_dll_t and the C pal_get_loaded_library declaration.
src/native/corehost/hostmisc/pal.windows.cAdded pal_get_loaded_library implementation and refactored module-path helper.
src/native/corehost/hostmisc/pal.windows.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/pal.unix.cAdded pal_get_loaded_library implementation (dlopen RTLD_NOLOAD + dladdr, with /proc/self/maps fallback).
src/native/corehost/hostmisc/pal.unix.cppC++ pal::get_loaded_library now delegates to pal_get_loaded_library.
src/native/corehost/hostmisc/CMakeLists.txtSplit hostmisc C vs C++ sources and introduced hostmisc_c object library.
src/native/corehost/fxr_resolver.hRemoved C++ wrapper surface; added C fxr_resolver_try_get_existing_fxr.
src/native/corehost/fxr_resolver.cppRemoved thin C++ wrapper implementation.
src/native/corehost/fxr_resolver.cAdded C implementation of fxr_resolver_try_get_existing_fxr.
src/native/corehost/CMakeLists.txtUpdated fxr_resolver interface target to stop compiling fxr_resolver.cpp.
src/native/corehost/apphost/static/CMakeLists.txtDropped ../../fxr_resolver.cpp from sources.
src/native/corehost/apphost/standalone/hostfxr_resolver.cppSwitched from C++ wrappers to C fxr_resolver_try_get_path API.
src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.csUpdated comment to match renamed enum reference (fxr_search_location).

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 4

Comment threadsrc/native/corehost/nethost/nethost.c
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/fxr_resolver.c Outdated
Comment threadsrc/native/corehost/fxr_resolver.c
- nethost.c: use %zu (not %d) for the size_t in the invalid-size trace.
- pal.unix.c: bound the /proc/self/maps %s read with an explicit field
width so a pathological line can't overflow the buffer.
- fxr_resolver.c: initialize *out_fxr in fxr_resolver_try_get_existing_fxr,
and drop the stale fxr_resolver.cpp reference from the file comment.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
It only held a c_fx_ver_t, so pass the c_fx_ver_t directly as the
pal_readdir_onlydirectories context instead of wrapping it.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 25, 2026 20:26

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings June 25, 2026 20:38

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/nethost/CMakeLists.txt
Comment threadsrc/native/corehost/apphost/standalone/hostfxr_resolver.cpp Outdated
Comment threadsrc/native/corehost/hostmisc/pal.unix.c
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Comment threadsrc/native/corehost/nethost/nethost.c Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
CopilotAI review requested due to automatic review settings June 30, 2026 15:27
In hostfxr_resolver_t's constructor, wrap the dotnet_root/fxr_path raw
pointers in a block so they go out of scope right after they are freed,
preventing later dangling-pointer use. The rest of the constructor only
needs the resolved flag and the assigned members.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

CopilotAI review requested due to automatic review settings June 30, 2026 15:35

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 2

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/fxr_resolver.h Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings July 1, 2026 20:06

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 1

Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h
Comment threadsrc/native/corehost/load_fxr_and_get_delegate.h Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
Co-authored-by: Elinor Fung <elfung@microsoft.com>
CopilotAI review requested due to automatic review settings July 1, 2026 22:48

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new

@elinor-fung
elinor-fung merged commit ad0692a into dotnet:mainJul 2, 2026
173 checks passed
@elinor-fung
elinor-fung deleted the nethost-to-c branch July 2, 2026 17:13
@dotnet-milestone-botdotnet-milestone-botBot added this to the 11.0-preview7 milestone Jul 3, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
Convert `nethost` to C. This builds on #128420 and #129480 to fully
convert `nethost` so it compiles and links as C-only — no C++ runtime
dependency.
- **pal**
- Add a C `pal_get_loaded_library` (look up an already-loaded library
without loading it). The C++ `pal::get_loaded_library` now delegates to
it.
- **fxr_resolver** - Convert the last C++-only piece, `try_get_existing_fxr`, to C.
- Remove the thin C++ wrappers in `fxr_resolver.cpp`
- Move `load_fxr_and_get_delegate` (used by `comhost` and `ijwhost`)
into its own header `load_fxr_and_get_delegate.h`
- **nethost**
- Replace `nethost.cpp` with `nethost.c` using the shared host bits that
have been converted to C. The RAII `error_writer_scope_t` is replaced by
manual save/restore of the error writer.
- **Build**
- Split `hostmisc` into C/C++ sources and add a C-only `hostmisc_c`
object library
**linux x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---|---|---|---|
| `libnethost.a` | 2,743,470 | 532,650 | -2,210,820 | -80.6% |
| `libnethost.so` | 43,376 | 35,072 | -8,304 | -19.1% |
**macOS arm64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|---:|---:|---:|---:|
| `libnethost.a` | 1,097,312 | 268,360 | −828,952 | −75.5% |
| `libnethost.dylib` | 92,368 | 89,088 | −3,280 | −3.6% |
**windows x64 Release**
| Binary | Baseline | PR | Δ bytes | Δ % |
|---|--:|--:|--:|--:|
| `libnethost.lib` | 2,951,176 | 1,677,420 | −1,273,756 | −43.2% |
| `nethost.dll` | 71,168 | 56,832 | −14,336 | −20.1% |
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Aug 3, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

@elinor-fung@AaronRobinsonMSFT