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>
221 lines
6.7 KiB
Markdown
221 lines
6.7 KiB
Markdown
---
|
|
id: wiki-2026-0508-link-seam-링크-접점
|
|
title: Link Seam (링크 접점)
|
|
category: 10_Wiki/Topics
|
|
status: verified
|
|
canonical_id: self
|
|
aliases: [Link Seam, Linker Seam]
|
|
duplicate_of: none
|
|
source_trust_level: A
|
|
confidence_score: 0.9
|
|
verification_status: applied
|
|
tags: [legacy-code, testing, seam, refactoring]
|
|
raw_sources: []
|
|
last_reinforced: 2026-05-10
|
|
github_commit: pending
|
|
tech_stack:
|
|
language: cpp
|
|
framework: none
|
|
---
|
|
|
|
# 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
|
|
1. **Preprocessing seam**: `#define`, `#ifdef` 의 compile-time substitution.
|
|
2. **Link seam**: linker 의 symbol resolution 의 test/prod binary 분리.
|
|
3. **Object seam**: virtual function / interface 의 polymorphic dispatch.
|
|
|
|
### 매 Link seam 의 enabling mechanism
|
|
- 매 production code 와 test code 가 동일 header / function signature 를 share.
|
|
- 매 production binary 는 real implementation `.o` link.
|
|
- 매 test binary 는 fake/stub implementation `.o` link.
|
|
- 매 source 파일 변경 없이 매 dependency 의 substitute.
|
|
|
|
### 매 응용
|
|
1. C/C++ legacy code 의 testability 도입 (most classic use case).
|
|
2. System call wrapping (LD_PRELOAD 의 fake `gettimeofday`).
|
|
3. Hardware abstraction layer (HAL) 의 test build.
|
|
4. Mock library injection (e.g., FFF in C, googlemock 의 link-time mock).
|
|
5. Embedded system unit testing on host.
|
|
|
|
## 💻 패턴
|
|
|
|
### 1. C link seam (canonical)
|
|
```c
|
|
// 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 분리
|
|
```cmake
|
|
# 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
|
|
```python
|
|
# 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)
|
|
```c
|
|
// 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)
|
|
```c
|
|
// 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)
|
|
```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)
|
|
```cpp
|
|
// 매 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 |
|