Efence 사용법

Efence(Electric Fence)는 메모리 관리와 디버깅을 위한 유용한 도구 중 하나로, 동적 메모리 할당과 관련된 버그를 찾고 디버깅하는 데 도움이 되며, 특히 메모리 오버런(Memory Overrun) 및 메모리 누수(Memory Leak)와 같은 문제를 식별하는 데 도움이 된다. 본 글에서는 Efence의 기본 개념, 사용법, 그리고 자주 사용하는 예제를 통해 Efence를 활용하는 방법에 대해 알아본다.

Efence 소개

Efence는 메모리 할당 및 해제에 관련된 버그를 찾는 데 사용되는 라이브러리이다. 주로 C 및 C++ 프로그램의 디버깅 과정에서 사용되며, 특히 동적 메모리 할당 및 해제에서 발생할 수 있는 문제를 감지한다. 이를 통해 런타임에 메모리 오버런, 누수, 해제 후 참조 등의 문제들을 찾아 낼 수 있다.

Efence 설치

Efence는 대부분의 리눅스 배포판에서 패키지로 제공되며, Ubuntu에서는 다음과 같이 설치할 수 있다.

$ sudo apt install electric-fence
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  electric-fence
0 upgraded, 1 newly installed, 0 to remove and 53 not upgraded.
Need to get 20.2 kB of archives.
After this operation, 78.8 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 electric-fence amd64 2.2.6 [20.2 kB]
Fetched 20.2 kB in 1s (17.1 kB/s)
Selecting previously unselected package electric-fence.
(Reading database ... 57178 files and directories currently installed.)
Preparing to unpack .../electric-fence_2.2.6_amd64.deb ...
Unpacking electric-fence (2.2.6) ...
Setting up electric-fence (2.2.6) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.6) ...

사용법

Efence를 사용하려면 프로그램을 컴파일하고 실행할 때 환경 변수를 설정해야 한다. 아래는 간단한 예제 프로그램을 통해 Efence를 사용하는 방법이다.

예제 프로그램

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr = (int*)malloc(5 * sizeof(int));
    arr[5] = 10;  // 배열 범위를 벗어나는 할당

    free(arr);    // 동적 메모리 해제

    return 0;
}

Efence로 컴파일 및 실행

Efence를 사용하려면 -lefence 옵션을 주고 컴파일 한다.

$ gcc -g my_program.c -lefence
$ ./a.out

  Electric Fence 2.2 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
Segmentation fault (core dumped)

컴파일 된 바이너리를 실행하면 Segmentation fault 가 발생하면서 core dump가 생성된다.

GDB로 efence 결과 확인

gdb를 사용하여 core dump된 파일로 위치를 확인할 수 있다.

$ gdb ./a.out core.a.out.1178.1709048429
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...
[New LWP 1178]

warning: Section `.reg-xstate/1178' in core file too small.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.

warning: Section `.reg-xstate/1178' in core file too small.
#0  main () at my_program.c:6
6           arr[5] = 10;  // 배열 범위를 벗어나는 할당
(gdb)

line 6 에서 배열 범위를 벗어난 메모리 접근으로 segmentation fault 이 발생 한 것을 알 수 있다.

참고 사이트

답글 남기기