Skip to content

Commit 8ee9ccf

Browse files
fix(kompile): map linked NormalSym functions to monoItemFn(noBody) (#953)
## Summary - Generate `MonoItemKind::MonoItemFn(symbol(...), defId(...), noBody)` for linked `NormalSym` entries that are present in `function_symbols` but missing from `items`. - Keep function resolution on concrete linked symbols instead of falling back to `"** UNKNOWN FUNCTION **"` for bodyless linked functions. - Refresh affected integration/show fixtures and normalize panic symbol hash suffixes in fixture comparison to keep snapshot output stable. - Preserve readable `kmir show --statistics --leaves` annotations for `noBody` callees (`>> function`, `>> call span`, `>> message`) even after replacing `"** UNKNOWN FUNCTION **"` with concrete symbols. ## Context In multi-crate SMIR, linked external functions can exist in `function_symbols` but not in local `items` (no local body). Before this change, that gap could leave `lookupFunction(Ty)` without a generated `monoItemFn` equation. ### Red vs Green **RED (actual stuck shape in show fixtures):** ```k #setUpCalleeData( monoItemFn(... name: symbol("** UNKNOWN FUNCTION **"), id: defId(38), body: noBody), ... ) ``` This is the unresolved-function path. **GREEN (after fix):** ```k #setUpCalleeData( monoItemFn(... name: symbol("_ZN4core9panicking5panic17h<hash>E"), id: defId(38), body: noBody), ... ) ``` The same path now resolves to a concrete linked symbol instead of `"** UNKNOWN FUNCTION **"`. --------- Co-authored-by: automergerpr-permission-manager[bot] <190534181+automergerpr-permission-manager[bot]@users.noreply.github.com>
1 parent 7258cfb commit 8ee9ccf

15 files changed

Lines changed: 427 additions & 61 deletions

kmir/src/kmir/kompile.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,25 @@ def _functions(kmir: KMIR, smir_info: SMIRInfo) -> dict[int, KInner]:
521521
for ty in smir_info.function_symbols_reverse[item_name]:
522522
functions[ty] = parsed_item_kinner.args[1]
523523

524-
# Add intrinsic functions
524+
# Add intrinsic functions and linked normal symbols that have no local body in `items`.
525+
# Normal symbols must still map to `monoItemFn(..., noBody)` instead of falling back to UNKNOWN FUNCTION.
525526
for ty, sym in smir_info.function_symbols.items():
526-
if 'IntrinsicSym' in sym and ty not in functions:
527+
if ty in functions:
528+
continue
529+
if 'IntrinsicSym' in sym:
527530
functions[ty] = KApply(
528531
'IntrinsicFunction',
529532
[KApply('symbol(_)_LIB_Symbol_String', [stringToken(sym['IntrinsicSym'])])],
530533
)
534+
elif isinstance(sym.get('NormalSym'), str):
535+
functions[ty] = KApply(
536+
'MonoItemKind::MonoItemFn',
537+
(
538+
KApply('symbol(_)_LIB_Symbol_String', (stringToken(sym['NormalSym']),)),
539+
KApply('defId(_)_BODY_DefId_Int', (intToken(ty),)),
540+
KApply('noBody_BODY_MaybeBody', ()),
541+
),
542+
)
531543

532544
return functions
533545

kmir/src/kmir/testing/fixtures.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import re
4+
import sys
35
from difflib import unified_diff
46
from typing import TYPE_CHECKING
57

@@ -13,24 +15,36 @@
1315

1416
from pytest import FixtureRequest, Parser
1517

16-
import sys
17-
1818

1919
def pytest_configure(config) -> None:
2020
sys.setrecursionlimit(1000000)
2121

2222

23+
def _normalize_symbol_hashes(text: str) -> str:
24+
"""Normalize rustc symbol hash suffixes that drift across builds/environments."""
25+
# Normalize mangled symbol hashes, including generic names with `$` and `.`.
26+
# Keep trailing `E` when present; truncated variants may omit it.
27+
text = re.sub(r'(_ZN[0-9A-Za-z_$.]+17h)[0-9a-fA-F]+E', r'\1<hash>E', text)
28+
text = re.sub(r'(_ZN[0-9A-Za-z_$.]+17h)[0-9a-fA-F]+', r'\1<hash>', text)
29+
# Normalize demangled hash suffixes (`...::h<hex>`).
30+
text = re.sub(r'(::h)[0-9a-fA-F]{8,}', r'\1<hash>', text)
31+
return text
32+
33+
2334
def assert_or_update_show_output(
2435
actual_text: str, expected_file: Path, *, update: bool, path_replacements: dict[str, str] | None = None
2536
) -> None:
2637
if path_replacements:
2738
for old, new in path_replacements.items():
2839
actual_text = actual_text.replace(old, new)
40+
# Normalize rustc symbol hash suffixes that can drift across builds/environments.
41+
actual_text = _normalize_symbol_hashes(actual_text)
2942
if update:
3043
expected_file.write_text(actual_text)
3144
else:
3245
assert expected_file.is_file()
3346
expected_text = expected_file.read_text()
47+
expected_text = _normalize_symbol_hashes(expected_text)
3448
if actual_text != expected_text:
3549
diff = '\n'.join(
3650
unified_diff(

kmir/src/kmir/utils.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ def _extract_alloc_id(operands: KInner) -> int | None:
288288
return None
289289

290290

291-
def _annotate_unknown_function(k_cell: KInner, smir_info: SMIRInfo) -> list[str]:
292-
"""If the k cell is `#setUpCalleeData` for `** UNKNOWN FUNCTION **`, return annotation lines with decoded info."""
291+
def _annotate_nobody_function(k_cell: KInner, smir_info: SMIRInfo) -> list[str]:
292+
"""If the k cell is `#setUpCalleeData` for a `noBody` callee, return annotation lines with decoded info."""
293293

294294
from .alloc import Allocation, AllocId, AllocInfo, Memory
295295
from .linker import _demangle
@@ -307,16 +307,14 @@ def _annotate_unknown_function(k_cell: KInner, smir_info: SMIRInfo) -> list[str]
307307
KApply(
308308
label=KLabel(name='MonoItemKind::MonoItemFn'),
309309
args=[
310-
KApply(args=[KToken(token=symbol_name)]),
310+
KApply(args=[KToken()]),
311311
KApply(label=KLabel(name='defId(_)_BODY_DefId_Int'), args=[KToken(token=def_id_str)]),
312312
_,
313313
],
314314
),
315315
operands,
316316
KApply(label=KLabel(name='span'), args=[KToken(token=span_str)]),
317-
] if (
318-
symbol_name == '\"** UNKNOWN FUNCTION **\"'
319-
):
317+
]:
320318
def_id = int(def_id_str)
321319
span = int(span_str)
322320
case _:
@@ -379,7 +377,7 @@ def render_leaf_k_cells(proof: APRProof, cterm_show: CTermShow, smir_info: SMIRI
379377
lines.extend(f' {k_line}' for k_line in k_lines)
380378

381379
if smir_info is not None and k_cell is not None:
382-
annotations = _annotate_unknown_function(k_cell, smir_info)
380+
annotations = _annotate_nobody_function(k_cell, smir_info)
383381
lines.extend(annotations)
384382

385383
if idx != len(leaves) - 1:

kmir/src/tests/integration/data/crate-tests/single-lib/small_test_lib::testing::test_add_in_range.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
┃ │
1717
┃ │ (6 steps)
1818
┃ └─ 6 (stuck, leaf)
19-
┃ #setUpCalleeData ( monoItemFn ( ... name: symbol ( "** UNKNOWN FUNCTION **" ) ,
19+
┃ #setUpCalleeData ( monoItemFn ( ... name: symbol ( "_ZN3std7process4exit17h<hash>
2020
2121
┗━━┓ subst: .Subst
2222
┃ constraint:

kmir/src/tests/integration/data/exec-smir/intrinsic/blackbox_function_symbols.expected.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
11
{
22
"-6": {
3-
"NormalSym": "_ZN4core3ptr85drop_in_place$LT$std..rt..lang_start$LT$$LP$$RP$$GT$..$u7b$$u7b$closure$u7d$$u7d$$GT$17hb524060ed0e83bbbE"
3+
"NormalSym": "_ZN4core3ptr85drop_in_place$LT$std..rt..lang_start$LT$$LP$$RP$$GT$..$u7b$$u7b$closure$u7d$$u7d$$GT$17h<hash>E"
44
},
55
"-5": {
6-
"NormalSym": "_ZN4core3ptr28drop_in_place$LT$$RF$u32$GT$17hb92e31f0aaa3d322E"
6+
"NormalSym": "_ZN4core3ptr28drop_in_place$LT$$RF$u32$GT$17h<hash>E"
77
},
88
"-4": {
9-
"NormalSym": "_ZN4core3ops8function6FnOnce40call_once$u7b$$u7b$vtable.shim$u7d$$u7d$17h526a5c5d4d9d3202E"
9+
"NormalSym": "_ZN4core3ops8function6FnOnce40call_once$u7b$$u7b$vtable.shim$u7d$$u7d$17h<hash>E"
1010
},
1111
"-3": {
12-
"NormalSym": "_ZN42_$LT$$RF$T$u20$as$u20$core..fmt..Debug$GT$3fmt17h3a8781eea2f9ddb7E"
12+
"NormalSym": "_ZN42_$LT$$RF$T$u20$as$u20$core..fmt..Debug$GT$3fmt17h<hash>E"
1313
},
1414
"-2": {
15-
"NormalSym": "_ZN3std2rt10lang_start17h17250425291430deE"
15+
"NormalSym": "_ZN3std2rt10lang_start17h<hash>E"
1616
},
1717
"-1": {
18-
"NormalSym": "_ZN8blackbox4main17h56268fefa1135d9eE"
18+
"NormalSym": "_ZN8blackbox4main17h<hash>E"
1919
},
2020
"0": {
21-
"NormalSym": "_ZN3std2rt19lang_start_internal17h018b8394ba015d86E"
21+
"NormalSym": "_ZN3std2rt19lang_start_internal17h<hash>E"
2222
},
2323
"13": {
24-
"NormalSym": "_ZN3std3sys9backtrace28__rust_begin_short_backtrace17h3ac302c9481e885fE"
24+
"NormalSym": "_ZN3std3sys9backtrace28__rust_begin_short_backtrace17h<hash>E"
2525
},
2626
"14": {
27-
"NormalSym": "_ZN54_$LT$$LP$$RP$$u20$as$u20$std..process..Termination$GT$6report17h8eaae5c69aab66e9E"
27+
"NormalSym": "_ZN54_$LT$$LP$$RP$$u20$as$u20$std..process..Termination$GT$6report17h<hash>E"
2828
},
2929
"19": {
30-
"NormalSym": "_ZN4core3ops8function6FnOnce9call_once17haae505bd39892fd2E"
30+
"NormalSym": "_ZN4core3ops8function6FnOnce9call_once17h<hash>E"
3131
},
3232
"20": {
3333
"IntrinsicSym": "black_box"
3434
},
3535
"21": {
36-
"NormalSym": "_ZN4core3fmt3num50_$LT$impl$u20$core..fmt..Debug$u20$for$u20$u32$GT$3fmt17h99c61f0bde9a2afdE"
36+
"NormalSym": "_ZN4core3fmt3num50_$LT$impl$u20$core..fmt..Debug$u20$for$u20$u32$GT$3fmt17h<hash>E"
3737
},
3838
"27": {
39-
"NormalSym": "_ZN4core3fmt3num53_$LT$impl$u20$core..fmt..LowerHex$u20$for$u20$u32$GT$3fmt17hb987357f13dc6cc8E"
39+
"NormalSym": "_ZN4core3fmt3num53_$LT$impl$u20$core..fmt..LowerHex$u20$for$u20$u32$GT$3fmt17h<hash>E"
4040
},
4141
"28": {
42-
"NormalSym": "_ZN4core3fmt3num53_$LT$impl$u20$core..fmt..UpperHex$u20$for$u20$u32$GT$3fmt17h7baa47f3e5cbe44cE"
42+
"NormalSym": "_ZN4core3fmt3num53_$LT$impl$u20$core..fmt..UpperHex$u20$for$u20$u32$GT$3fmt17h<hash>E"
4343
},
4444
"29": {
45-
"NormalSym": "_ZN4core3fmt3num3imp52_$LT$impl$u20$core..fmt..Display$u20$for$u20$u32$GT$3fmt17hec74c53b91325b16E"
45+
"NormalSym": "_ZN4core3fmt3num3imp52_$LT$impl$u20$core..fmt..Display$u20$for$u20$u32$GT$3fmt17h<hash>E"
4646
},
4747
"30": {
48-
"NormalSym": "_ZN4core3ops8function6FnOnce9call_once17h2d8e0aae13049ae8E"
48+
"NormalSym": "_ZN4core3ops8function6FnOnce9call_once17h<hash>E"
4949
},
5050
"32": {
51-
"NormalSym": "_ZN3std2rt10lang_start28_$u7b$$u7b$closure$u7d$$u7d$17h5c121846c1652782E"
51+
"NormalSym": "_ZN3std2rt10lang_start28_$u7b$$u7b$closure$u7d$$u7d$17h<hash>E"
5252
},
5353
"35": {
5454
"IntrinsicSym": "black_box"
5555
},
5656
"36": {
57-
"NormalSym": "_ZN4core9panicking19assert_failed_inner17h1d286061ca0adfe7E"
57+
"NormalSym": "_ZN4core9panicking19assert_failed_inner17h<hash>E"
5858
},
5959
"43": {
60-
"NormalSym": "_ZN8blackbox7add_one17h19d5f3b41fdf13daE"
60+
"NormalSym": "_ZN8blackbox7add_one17h<hash>E"
6161
},
6262
"44": {
63-
"NormalSym": "_ZN4core4hint9black_box17h0bfc654765aa2ddfE"
63+
"NormalSym": "_ZN4core4hint9black_box17h<hash>E"
6464
},
6565
"45": {
66-
"NormalSym": "_ZN4core9panicking13assert_failed17h9acd0d94a91ca0eaE"
66+
"NormalSym": "_ZN4core9panicking13assert_failed17h<hash>E"
6767
},
6868
"48": {
6969
"NoOpSym": ""

kmir/src/tests/integration/data/exec-smir/intrinsic/blackbox_function_symbols_reverse.expected.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
11
{
2-
"_ZN3std2rt10lang_start17h17250425291430deE": [
2+
"_ZN3std2rt10lang_start17h<hash>E": [
33
-2
44
],
5-
"_ZN3std2rt10lang_start28_$u7b$$u7b$closure$u7d$$u7d$17h5c121846c1652782E": [
5+
"_ZN3std2rt10lang_start28_$u7b$$u7b$closure$u7d$$u7d$17h<hash>E": [
66
32
77
],
8-
"_ZN3std2rt19lang_start_internal17h018b8394ba015d86E": [
8+
"_ZN3std2rt19lang_start_internal17h<hash>E": [
99
0
1010
],
11-
"_ZN3std3sys9backtrace28__rust_begin_short_backtrace17h3ac302c9481e885fE": [
11+
"_ZN3std3sys9backtrace28__rust_begin_short_backtrace17h<hash>E": [
1212
13
1313
],
14-
"_ZN42_$LT$$RF$T$u20$as$u20$core..fmt..Debug$GT$3fmt17h3a8781eea2f9ddb7E": [
14+
"_ZN42_$LT$$RF$T$u20$as$u20$core..fmt..Debug$GT$3fmt17h<hash>E": [
1515
-3
1616
],
17-
"_ZN4core3fmt3num3imp52_$LT$impl$u20$core..fmt..Display$u20$for$u20$u32$GT$3fmt17hec74c53b91325b16E": [
17+
"_ZN4core3fmt3num3imp52_$LT$impl$u20$core..fmt..Display$u20$for$u20$u32$GT$3fmt17h<hash>E": [
1818
29
1919
],
20-
"_ZN4core3fmt3num50_$LT$impl$u20$core..fmt..Debug$u20$for$u20$u32$GT$3fmt17h99c61f0bde9a2afdE": [
20+
"_ZN4core3fmt3num50_$LT$impl$u20$core..fmt..Debug$u20$for$u20$u32$GT$3fmt17h<hash>E": [
2121
21
2222
],
23-
"_ZN4core3fmt3num53_$LT$impl$u20$core..fmt..LowerHex$u20$for$u20$u32$GT$3fmt17hb987357f13dc6cc8E": [
23+
"_ZN4core3fmt3num53_$LT$impl$u20$core..fmt..LowerHex$u20$for$u20$u32$GT$3fmt17h<hash>E": [
2424
27
2525
],
26-
"_ZN4core3fmt3num53_$LT$impl$u20$core..fmt..UpperHex$u20$for$u20$u32$GT$3fmt17h7baa47f3e5cbe44cE": [
26+
"_ZN4core3fmt3num53_$LT$impl$u20$core..fmt..UpperHex$u20$for$u20$u32$GT$3fmt17h<hash>E": [
2727
28
2828
],
29-
"_ZN4core3ops8function6FnOnce40call_once$u7b$$u7b$vtable.shim$u7d$$u7d$17h526a5c5d4d9d3202E": [
29+
"_ZN4core3ops8function6FnOnce40call_once$u7b$$u7b$vtable.shim$u7d$$u7d$17h<hash>E": [
3030
-4
3131
],
32-
"_ZN4core3ops8function6FnOnce9call_once17h2d8e0aae13049ae8E": [
32+
"_ZN4core3ops8function6FnOnce9call_once17h<hash>E": [
3333
30
3434
],
35-
"_ZN4core3ops8function6FnOnce9call_once17haae505bd39892fd2E": [
35+
"_ZN4core3ops8function6FnOnce9call_once17h<hash>E": [
3636
19
3737
],
38-
"_ZN4core3ptr28drop_in_place$LT$$RF$u32$GT$17hb92e31f0aaa3d322E": [
38+
"_ZN4core3ptr28drop_in_place$LT$$RF$u32$GT$17h<hash>E": [
3939
-5
4040
],
41-
"_ZN4core3ptr85drop_in_place$LT$std..rt..lang_start$LT$$LP$$RP$$GT$..$u7b$$u7b$closure$u7d$$u7d$$GT$17hb524060ed0e83bbbE": [
41+
"_ZN4core3ptr85drop_in_place$LT$std..rt..lang_start$LT$$LP$$RP$$GT$..$u7b$$u7b$closure$u7d$$u7d$$GT$17h<hash>E": [
4242
-6
4343
],
44-
"_ZN4core4hint9black_box17h0bfc654765aa2ddfE": [
44+
"_ZN4core4hint9black_box17h<hash>E": [
4545
44
4646
],
47-
"_ZN4core9panicking13assert_failed17h9acd0d94a91ca0eaE": [
47+
"_ZN4core9panicking13assert_failed17h<hash>E": [
4848
45
4949
],
50-
"_ZN4core9panicking19assert_failed_inner17h1d286061ca0adfe7E": [
50+
"_ZN4core9panicking19assert_failed_inner17h<hash>E": [
5151
36
5252
],
53-
"_ZN54_$LT$$LP$$RP$$u20$as$u20$std..process..Termination$GT$6report17h8eaae5c69aab66e9E": [
53+
"_ZN54_$LT$$LP$$RP$$u20$as$u20$std..process..Termination$GT$6report17h<hash>E": [
5454
14
5555
],
56-
"_ZN8blackbox4main17h56268fefa1135d9eE": [
56+
"_ZN8blackbox4main17h<hash>E": [
5757
-1
5858
],
59-
"_ZN8blackbox7add_one17h19d5f3b41fdf13daE": [
59+
"_ZN8blackbox7add_one17h<hash>E": [
6060
43
6161
],
6262
"black_box": [

0 commit comments

Comments
 (0)