<style> .markdown-body b { font-weight: 700; } .markdown-body code, code, pre { max-height:80% !important; font-size: 14px; line-height: 115%; color: #99999 !important; background-color: #ffe9e5; } </style> # 資安報告! <!-- Put the link to this slide here so people can follow --> <!-- slide: https://hackmd.io/p/template-Talk-slide --> --- ## Who are you? nobody --- ## concurrency rule(並行性規則) [CON32-C](https://wiki.sei.cmu.edu/confluence/display/c/CON32-C.+Prevent+data+races+when+accessing+bit-fields+from+multiple+threads) [CON33-C](https://wiki.sei.cmu.edu/confluence/display/c/CON33-C.+Avoid+race+conditions+when+using+library+functions) --- ## CON32-C Prevent data races when accessing bit-fields from multiple threads. --- ### race conditions ```c #include <stdio.h> #include <pthread.h> #define NUM_THREADS 8 int counter=0; void *threadFunction(void *threadId) { long tid = (long)threadId; for (int i = 0; i < 10000; i++) { counter++; } printf("Thread %ld: Counter value: %lu\n", tid, counter); pthread_exit(NULL); } int main() { pthread_t threads[NUM_THREADS]; int i; for (i = 0; i < NUM_THREADS; i++) { if (0 != pthread_create(&threads[i], NULL, threadFunction, (void *)(long)i)) { printf("Error creating thread %d\n", i); return 1; } } for (i = 0; i < NUM_THREADS; i++) { pthread_join(threads[i], NULL); } printf("Final counter value: %ld\n", counter); return 0; } ``` --- ### 輸出 <style> .right_img { /* width: 100%; */ width: 35%; position: absolute; top: 10%; right:5%; z-index:10; } </style> <img class ="right_img" src="https://blogger.googleusercontent.com/img/a/AVvXsEgyjOAxskQj7NJoKyl07SJnfTu6NuuVVWZdMV9TZVb0kigS0Hbi9KNEJHambOOeKMq-qavL2vX0nbo5pNUB7YPK6oGQuNGSoHMWkLtWkRqvEFmk8WEsWYSYU6-SA2UDAQmt6tbUG8225MqQjk75jdpwtfmFZX9nfkgdh-8ESaqAX1x5Bs1EbGoi4iMoVw=w640-h472"> ``` Thread 0: Counter value: 10000 Thread 4: Counter value: 20000 Thread 1: Counter value: 30000 Thread 2: Counter value: 40165 Thread 3: Counter value: 48295 Thread 5: Counter value: 58295 Thread 7: Counter value: 68295 Thread 6: Counter value: 75784 Final counter value: 75784 ``` --- ### bit-fields ```c struct foo { int a : 1; int b : 2; unsigned int c : 3; unsigned int d : 4; }; int main() { struct foo f; f.a=1; f.b=1; f.c=3; f.d=3; printf("a=%d\nb=%d\nc=%d\nd=%d\n", f.a, f.b, f.c, f.d); // a=-1 // b=1 // c=3 // d=3 return 0; } ``` --- ### zero-width bit field ```c struct foo { unsigned int a : 3; unsigned int b : 2; unsigned int : 0; /* Force alignment to next boundary */ unsigned int c : 4; unsigned int d : 3; }; int main() { int i = 0xFFFF; struct foo *f = (struct foo *) &i; printf("a=%d\nb=%d\nc=%d\nd=%d\n", f->a, f->b, f->c, f->d); //a=7 b=3 c=12 d=2 return 0; } ``` ``` 0000 0000 0000 0000 1111 1111 1111 1111 b baaa dddcccc |← int 32 bits →|← int 32 bits →| ``` --- ### mutex ```c #include <stdio.h> #include <pthread.h> #define NUM_THREADS 8 int counter = 0; pthread_mutex_t mutex; void *threadFunction(void *threadId) { long tid = (long)threadId; for (int i = 0; i < 100000; i++) { pthread_mutex_lock(&mutex); counter++; pthread_mutex_unlock(&mutex); } printf("Thread %ld: Counter value: %d\n", tid, counter); pthread_exit(NULL); } int main() { pthread_t threads[NUM_THREADS]; int i; pthread_mutex_init(&mutex, NULL); for (i = 0; i < NUM_THREADS; i++) { int result = pthread_create(&threads[i], NULL, threadFunction, (void *)(long)i); if (result != 0) { printf("Error creating thread %d\n", i); return 1; } } for (i = 0; i < NUM_THREADS; i++) { pthread_join(threads[i], NULL); } pthread_mutex_destroy(&mutex); printf("Final counter value: %d\n", counter); return 0; } ``` --- ### mutex 輸出 <style> .right_img1 { /* height: 45%; */ width:40%; position: absolute; top: 20%; right:5%; z-index:10; { </style> <img class="right_img1" src ="https://forums.autodesk.com/t5/image/serverpage/image-id/344684iCB014721B542E874?v=v2"> ``` Thread 5: Counter value: 284318 Thread 7: Counter value: 618112 Thread 4: Counter value: 716860 Thread 1: Counter value: 740933 Thread 3: Counter value: 753300 Thread 2: Counter value: 799071 Thread 0: Counter value: 800000 Thread 6: Counter value: 799140 Final counter value: 800000 ``` --- ## CON33-C Avoid race conditions when using library functions. --- ## not thread safe library functions ``` rand(), srand() getenv(), getenv_s() strtok() strerror() asctime(), ctime(), localtime(), gmtime() asctime_s(), ctime_s(), localtime_s(), gmtime_s() setlocale() ``` --- ## rand ```c #include <stdio.h> #include <pthread.h> #include <stdlib.h> #define NUM_THREADS 8 int counter=0; void *threadFunction(void *threadId) { long tid = (long)threadId; int a= rand(); for (int i = 0; i < 10000; i++) { counter+=a; } printf("Thread %ld: Sum value: %lu\n", tid, counter); pthread_exit(NULL); } int main() { pthread_t threads[NUM_THREADS]; int i; for (i = 0; i < NUM_THREADS; i++) { if (0 != pthread_create(&threads[i], NULL, threadFunction, (void *)(long)i)) { printf("Error creating thread %d\n", i); return 1; } } for (i = 0; i < NUM_THREADS; i++) { pthread_join(threads[i], NULL); } printf("Final Sum value: %ld\n", counter); return 0; } ``` --- ## rand output first time ``` Thread 1: Sum value: 4031186800 Thread 2: Sum value: 3664539088 Thread 3: Sum value: 1500377952 Thread 0: Sum value: 2360082320 Thread 4: Sum value: 4160134042 Thread 6: Sum value: 3635315610 Thread 7: Sum value: 4123987514 Thread 5: Sum value: 464556282 Final Sum value: 464556282 ``` second time ``` Thread 0: Sum value: 4031186800 Thread 1: Sum value: 3664539088 Thread 2: Sum value: 1500377952 Thread 4: Sum value: 2360082320 Thread 5: Sum value: 3377077152 Thread 3: Sum value: 2332738704 Thread 7: Sum value: 2821410608 Thread 6: Sum value: 3456946672 Final Sum value: 3456946672 ``` --- ## Done ![](https://img.devrant.com/devrant/rant/r_88296_U3rex.jpg)
{"metaMigratedAt":"2023-06-18T04:08:59.525Z","metaMigratedFrom":"YAML","title":"資安報告","breaks":true,"description":"View the slide with \"Slide Mode\".","contributors":"[{\"id\":\"88becfca-8111-457c-985b-2a052473a787\",\"add\":12321,\"del\":8133}]"}
    356 views