f8b21af4be
10_Wiki/Topics 대규모 정리: - 오류 캡처/미완성 stub 문서 227개 제거 - 교차폴더 중복 43클러스터 병합 (63파일 → redirect) - 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건 - 카테고리 MOC 6개 신규 생성 - Graph 섹션 미해결 related-keyword 링크 10,058건 제거 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
6.7 KiB
6.7 KiB
id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
| id | title | category | status | canonical_id | aliases | duplicate_of | source_trust_level | confidence_score | verification_status | tags | raw_sources | last_reinforced | github_commit | tech_stack | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| wiki-2026-0508-link-seam-링크-접점 | Link Seam (링크 접점) | 10_Wiki/Topics | verified | self |
|
none | A | 0.9 | applied |
|
2026-05-10 | pending |
|
Link Seam (링크 접점)
매 한 줄
"매 linker 의 symbol resolution 을 test 의 enabling point 로 활용". Michael Feathers, Working Effectively with Legacy Code (2004) — 매 source code 변경 없이 link-time 의 symbol substitution 으로 dependency 를 break 하는 seam. C/C++ 의 native 기법, 2026 에서는 매 dynamic library injection (LD_PRELOAD), test double linking, 또는 Bazel/CMake test target 분리 로 활용.
매 핵심
매 Feathers 의 3 seam types
- Preprocessing seam:
#define,#ifdef의 compile-time substitution. - Link seam: linker 의 symbol resolution 의 test/prod binary 분리.
- Object seam: virtual function / interface 의 polymorphic dispatch.
매 Link seam 의 enabling mechanism
- 매 production code 와 test code 가 동일 header / function signature 를 share.
- 매 production binary 는 real implementation
.olink. - 매 test binary 는 fake/stub implementation
.olink. - 매 source 파일 변경 없이 매 dependency 의 substitute.
매 응용
- C/C++ legacy code 의 testability 도입 (most classic use case).
- System call wrapping (LD_PRELOAD 의 fake
gettimeofday). - Hardware abstraction layer (HAL) 의 test build.
- Mock library injection (e.g., FFF in C, googlemock 의 link-time mock).
- Embedded system unit testing on host.
💻 패턴
1. C link seam (canonical)
// time_provider.h
#ifndef TIME_PROVIDER_H
#define TIME_PROVIDER_H
long current_time_ms(void);
#endif
// production: time_provider_real.c
#include <time.h>
long current_time_ms(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
// test: time_provider_fake.c
static long fake_now = 0;
long current_time_ms(void) { return fake_now; }
void set_fake_time(long t) { fake_now = t; }
// Production link
// gcc app.c time_provider_real.c -o app
// Test link
// gcc test_app.c time_provider_fake.c -o test_app
2. CMake target 분리
# CMakeLists.txt
add_library(time_real STATIC time_provider_real.c)
add_library(time_fake STATIC time_provider_fake.c)
add_executable(app main.c)
target_link_libraries(app PRIVATE time_real)
add_executable(app_test test_main.c)
target_link_libraries(app_test PRIVATE time_fake)
add_test(NAME app_test COMMAND app_test)
3. Bazel test seam
# BUILD
cc_library(
name = "time_real",
srcs = ["time_provider_real.c"],
hdrs = ["time_provider.h"],
)
cc_library(
name = "time_fake",
srcs = ["time_provider_fake.c"],
hdrs = ["time_provider.h"],
testonly = True,
)
cc_binary(name = "app", srcs = ["main.c"], deps = [":time_real"])
cc_test(name = "app_test", srcs = ["test_main.c"], deps = [":time_fake"])
4. LD_PRELOAD (runtime link seam)
// fake_malloc.c
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stddef.h>
static int allocation_count = 0;
int get_alloc_count(void) { return allocation_count; }
void *malloc(size_t size) {
static void *(*real_malloc)(size_t) = NULL;
if (!real_malloc) real_malloc = dlsym(RTLD_NEXT, "malloc");
allocation_count++;
return real_malloc(size);
}
// Compile: gcc -shared -fPIC fake_malloc.c -ldl -o libfakemalloc.so
// Run: LD_PRELOAD=./libfakemalloc.so ./my_program
5. Weak symbol seam (GCC)
// production_default.c
__attribute__((weak)) int sensor_read(void) {
return read_real_sensor();
}
// test_override.c
int sensor_read(void) {
return 42; // strong symbol overrides weak
}
// Test build links test_override.c → uses fake.
// Prod build doesn't link test_override.c → uses weak default.
6. FFF (Fake Function Framework, C)
#include "fff.h"
DEFINE_FFF_GLOBALS;
// Header declares: void send_packet(const char* data);
FAKE_VOID_FUNC(send_packet, const char*);
void test_send(void) {
RESET_FAKE(send_packet);
do_thing();
assert(send_packet_fake.call_count == 1);
}
// Link test binary with fakes instead of real send_packet.
7. Migration to object seam (preferred long-term)
// 매 link seam 은 stepping stone
// 매 long-term: interface + DI 로 evolve
class TimeProvider {
public:
virtual long now_ms() = 0;
virtual ~TimeProvider() = default;
};
class RealTimeProvider : public TimeProvider {
long now_ms() override { /* real */ }
};
class FakeTimeProvider : public TimeProvider {
public:
long fake_now = 0;
long now_ms() override { return fake_now; }
};
// Now no link tricks needed — just inject.
매 결정 기준
| 상황 | Approach |
|---|---|
| Legacy C code, 매 interface 도입 어려움 | Link seam. |
| C++ codebase, 매 refactor 가능 | Object seam (virtual / interface). |
| System call / library function 의 mocking | LD_PRELOAD link seam. |
| Embedded HAL의 host testing | Link seam (fake HAL implementation). |
| Modern code, full DI | Object seam — link seam 불필요. |
| Single-binary monolith 의 sub-module test | Link seam 또는 build target 분리. |
기본값: 새 C/C++ 코드는 매 object seam 으로 시작. Link seam 은 매 legacy 의 testability rescue tool.
🔗 Graph
- 변형: Preprocessing Seam · Object Seam
- 응용: Test Doubles
- Adjacent: Dependency Injection
🤖 LLM 활용
언제: legacy C/C++ codebase 의 unit test 도입, system call mocking, embedded HAL host test. 언제 X: 매 modern dynamic language (Python/JS/Ruby — monkey patch 충분), 매 fresh codebase (object seam 이 cleaner).
❌ 안티패턴
- Production binary 에 test fake 누설: 매 build target separation 실패 시 매 prod 가 fake link. 매 strict CMake/Bazel target 분리.
- Symbol collision: 매 multiple translation unit 에서 동일 symbol — undefined behavior.
- LD_PRELOAD 의 prod 사용: 매 debugging 도구로만, 매 production deploy 의 anti-pattern.
- Header/source mismatch: 매 fake 가 매 signature 변경 의 stale — 매 ABI mismatch crash.
- Link seam 의 영구화: 매 stepping stone, 매 object seam 으로 evolve 의 missing.
🧪 검증 / 중복
- Verified (Feathers, Working Effectively with Legacy Code 2004, Ch.4 "The Seam Model").
- 신뢰도 A.
🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — Feathers seam taxonomy + CMake/Bazel/LD_PRELOAD modern application |