簡體   English   中英

pthread_mutex_lock() 的段錯誤

[英]segfault at pthread_mutex_lock()

我有一個打印一些數據結構的代碼。

void
print_wheel_timer(wheel_timer_t *wt){

 <code to print data structure>
}

Then i sandwiched the code in-between lock and unlock mutex calls. 

void
print_wheel_timer(wheel_timer_t *wt){
pthread_mutex_lock(&wt->global_lock);
<code to print data structure>
pthread_mutex_unlock(&wt->global_lock);
}

而現在,它出現了段錯誤。 我已經禁用了代碼中的所有線程,現在程序是單線程的,仍然是段錯誤。 我正在使用 gcc、ubuntu 19.04。

gdb 顯示pthread_mutex_lock()調用正在搞砸數據結構,! 這很奇怪! 我將觀察點放在由任意值寫入的地址上,它顯示對pthread_mutex_lock()的調用正在修改它。 當我刪除這些鎖定/解鎖調用時,沒有看到段錯誤。

(gdb) watch *(glthread_t *)&(wt->slotlist[1].slots.right)
Hardware watchpoint 2: *(glthread_t *)&(wt->slotlist[1].slots.right)
(gdb) c
Continuing.
Printing Wheel Timer DS

Thread 1 "test.exe" hit Hardware watchpoint 2: *(glthread_t *)&(wt->slotlist[1].slots.right)

Old value = {left = 0x0, right = 0x0}
New value = {left = 0x1, right = 0x0}     <<< corruption !!
0x00007ffff7fa3934 in __GI___pthread_mutex_lock (mutex=0x5555555771d8)
at ../nptl/pthread_mutex_lock.c:80
80      ../nptl/pthread_mutex_lock.c: No such file or directory.
(gdb) bt
#0  0x00007ffff7fa3934 in __GI___pthread_mutex_lock (mutex=0x5555555771d8)
at ../nptl/pthread_mutex_lock.c:80
#1  0x000055555555f3a2 in print_wheel_timer (wt=0x555555577180)
at WheelTimer/WheelTimer.c:276

Code :
========
void
print_wheel_timer(wheel_timer_t *wt){

int i = 0, j = 0;
glthread_t *curr;
glthread_t *slot_list_head = NULL;
wheel_timer_elem_t *wt_elem = NULL;

printf("Printing Wheel Timer DS\n");
pthread_mutex_lock(&wt->global_lock);
printf("wt->current_clock_tic  = %d\n", wt->current_clock_tic);
printf("wt->clock_tic_interval = %d\n", wt->clock_tic_interval);
printf("wt->wheel_size         = %d\n", wt->wheel_size);
printf("wt->current_cycle_no   = %d\n", wt->current_cycle_no);
printf("wt->wheel_thread       = %p\n", &wt->wheel_thread);
printf("WT uptime              = %s\n", hrs_min_sec_format(WT_UPTIME(wt)));
printf("wt->no_of_wt_elem      = %u\n", wt->no_of_wt_elem);
printf("printing slots : \n");

for(; i < wt->wheel_size; i++){
    slot_list_head = WT_SLOTLIST_HEAD(wt, i);
    ITERATE_GLTHREAD_BEGIN(slot_list_head, curr){           << segfaulting here for i = 1
        wt_elem = glthread_to_wt_elem(curr);

如果某個神聖的靈魂想在他的機器上嘗試一下。 請在此處下載代碼: https://github.com/sachinites/tcpip_stack

After downloading :
switch to branch "Hellos"
git checkout Hellos
Compile :
make all
run
./test.exe
to reproduce , run the cmd : 
debug show node H1 timer 

現在它將以 function 為核心:在WheelTimer/WheelTimer.c中實現print_wheel_timer()

問題出在我的數據結構上,其中零長度數組是 C 結構的非最后一個成員,當讀取/寫入此類 arrays 時,它會損壞 memory。 可拉伸 arrays 應該是結構的最后一個成員。

typedef struct _wheel_timer_t {
    int current_clock_tic;
    int clock_tic_interval;
    int wheel_size;
    int current_cycle_no;
    pthread_t wheel_thread;
    slotlist_t slotlist[0];            << problem, make it last member
    slotlist_t reschd_list;
    unsigned int no_of_wt_elem;
    pthread_mutex_t global_lock;
} wheel_timer_t;

幾乎可以肯定,您的wheel_timer_t結構具有類型為pthread_mutex_t *而不是pthread_mutex_tglobal_lock成員,這意味着您將錯誤類型的指針傳遞給pthread_mutex_lock ,該指針僅指向 8 個字節的存儲空間(假設為 64 位實現)。 如果您使用編譯器警告,這將被檢測到; pthread_mutex_t **不能隱式轉換為pthread_mutex_t *並且編譯器應該對此發出診斷。

請注意,這可能意味着您還有其他重大錯誤,並且您的互斥鎖未正確初始化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM