Upgrade to PRO for Only $50/Yearโ€”Limited-Time Offer! ๐Ÿ”ฅ

The Rust Programming Language

The Rust Programmingย Language

2015 spring, UPnL workshop.

Avatar for Hyeon Kim

Hyeon Kim

May 09, 2015
Tweet

More Decks by Hyeon Kim

Other Decks in Programming

Transcript

  1. malloc(), free() โ€ข ์›ํ•˜๋Š” ์–‘์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น, ํ”„๋กœ๊ทธ๋ž˜๋จธ์—๊ฒŒ ์ค€๋‹ค โ€ข

    ํ”„๋กœ๊ทธ๋ž˜๋จธ์—๊ฒ ์ •๋ฆฌ์˜ ์˜๋ฌด๊ฐ€ ์žˆ์Œ void *malloc(size_t size); void free(void *ptr)
  2. void *mem1 = malloc(100); void *mem2 = malloc(1000); /* ...

    */ if (some_problem) { free(mem1); free(mem2); return; } /* ... */ void *mem3 = malloc(10000); if (another_problem) { free(mem1); free(mem2); free(mem3); return; } /* ... */ if (yet_another_problem) { free(mem1); free(mem2); free(mem3); return; } /* ... */ free(mem1); free(mem2); free(mem3);
  3. void *mem1 = malloc(100); void *mem2 = malloc(1000); /* ...

    */ if (some_problem) { free(mem1); free(mem2); return; } /* ... */ void *mem3 = malloc(10000); if (another_problem) { free(mem1); free(mem2); free(mem3); return; } /* ... */ if (yet_another_problem) { free(mem1); free(mem2); free(mem3); return; } /* ... */ free(mem1); free(mem2); free(mem3);
  4. void *mem1 = malloc(100); void *mem2 = malloc(1000); /* ...

    */ if (some_problem) { free(mem1); free(mem2); return; } /* ... */ void *mem3 = malloc(10000); if (another_problem) { free(mem1); free(mem2); free(mem3); return; } /* ... */ if (yet_another_problem) { free(mem1); free(mem2); free(mem3); return; } /* ... */ free(mem1); free(mem2); free(mem3);
  5. void *mem1 = malloc(100); void *mem2 = malloc(1000); /* ...

    */ if (some_problem) { free(mem1); free(mem2); return; } /* ... */ void *mem3 = malloc(10000); if (another_problem) { free(mem1); free(mem2); free(mem3); return; } /* ... */ if (yet_another_problem) { free(mem1); free(mem2); free(mem3); return; } /* ... */ free(mem1); free(mem2); free(mem3);
  6. ์‚ฌ์‹ค ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋จ โ€ข ๋ฌธ์ œ ํ•ด๊ฒฐ? void *mem1 =

    malloc(100); void *mem2 = 0; void *mem3 = 0; /* ... */ if (some_problem) { goto ERROR; } mem2 = malloc(1000); /* ... */ mem3 = malloc(10000); if (another_problem) { goto ERROR; } /* ... */ if (yet_another_problem) { goto ERROR; } /* ... */ ERROR: free(mem1); mem1 = 0; free(mem2); mem2 = 0; free(mem3); mem3 = 0;
  7. ์‚ฌ์‹ค ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋จ โ€ข ๋ฌธ์ œ ํ•ด๊ฒฐ? โ€ข malloc(), free()

    ์™ธ์— ๋‹ค๋ฅธ ๋ฆฌ์†Œ ์Šค ํ• ๋‹น์„ ์Šคํƒ ์Œ“๋“ฏ์ด ํ•˜๋ฉด? โ€ข double-free, use-after-free ์— ์„œ ์•ˆ์ „ํ•œ๊ฐ€? โ€ข ์—ฌ์ „ํžˆ free ๊นŒ๋จน๊ณ  ์•ˆํ•  ๊ฐ€๋Šฅ์„ฑ void *mem1 = malloc(100); void *mem2 = 0; void *mem3 = 0; /* ... */ if (some_problem) { goto ERROR; } mem2 = malloc(1000); /* ... */ mem3 = malloc(10000); if (another_problem) { goto ERROR; } /* ... */ if (yet_another_problem) { goto ERROR; } /* ... */ ERROR: free(mem1); mem1 = 0; free(mem2); mem2 = 0; free(mem3); mem3 = 0;
  8. ์‚ฌ์‹ค ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋จ โ€ข ๋ฌธ์ œ ํ•ด๊ฒฐ? โ€ข malloc(), free()

    ์™ธ์— ๋‹ค๋ฅธ ๋ฆฌ์†Œ ์Šค ํ• ๋‹น์„ ์Šคํƒ ์Œ“๋“ฏ์ด ํ•˜๋ฉด? โ€ข double-free, use-after-free ์— ์„œ ์•ˆ์ „ํ•œ๊ฐ€? โ€ข ์—ฌ์ „ํžˆ free ๊นŒ๋จน๊ณ  ์•ˆํ•  ๊ฐ€๋Šฅ์„ฑ void *mem1 = malloc(100); void *mem2 = 0; void *mem3 = 0; /* ... */ if (some_problem) { goto ERROR; } mem2 = malloc(1000); /* ... */ mem3 = malloc(10000); if (another_problem) { goto ERROR; } /* ... */ if (yet_another_problem) { goto ERROR; } /* ... */ ERROR: free(mem1); mem1 = 0; free(mem2); mem2 = 0; free(mem3); mem3 = 0;
  9. ์‚ฌ์‹ค ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋จ โ€ข ๋ฌธ์ œ ํ•ด๊ฒฐ? โ€ข malloc(), free()

    ์™ธ์— ๋‹ค๋ฅธ ๋ฆฌ์†Œ ์Šค ํ• ๋‹น์„ ์Šคํƒ ์Œ“๋“ฏ์ด ํ•˜๋ฉด? โ€ข double-free, use-after-free ์— ์„œ ์•ˆ์ „ํ•œ๊ฐ€? โ€ข ์—ฌ์ „ํžˆ free ๊นŒ๋จน๊ณ  ์•ˆํ•  ๊ฐ€๋Šฅ์„ฑ void *mem1 = malloc(100); void *mem2 = 0; void *mem3 = 0; /* ... */ if (some_problem) { goto ERROR; } mem2 = malloc(1000); /* ... */ mem3 = malloc(10000); if (another_problem) { goto ERROR; } /* ... */ if (yet_another_problem) { goto ERROR; } /* ... */ ERROR: free(mem1); mem1 = 0; free(mem2); mem2 = 0; free(mem3); mem3 = 0;
  10. C++, RAII โ€ข ์†Œ๋ฉธ์ž ๋ฌธ๋ฒ• โ€ข ๊ด„ํ˜ธ๊ฐ€ ์—ด๋ฆฌ๋ฉด, ๋‹ซํž๊ฒƒ์ด ๋ฌด์กฐ๊ฑด

    ๋ณด์žฅ๋จ struct raii { raii() { hello(); } ~raii() { bye(); } }; int main() { for (int a = 0; a < 10; ++a) { raii a; // ์ž๋™์œผ๋กœ hello() ๊ฐ€ ํ˜ธ์ถœ๋จ /* ... */ } // ์Šค์ฝ”ํ”„๋ฅผ ๋น ์ ธ๋‚˜๊ฐ€๋ฉด ์ž๋™์œผ๋กœ bye() ๊ฐ€ ํ˜ธ์ถœ๋จ }
  11. ์Šค๋งˆํŠธ ํฌ์ธํ„ฐ โ€ข ๋˜‘๋˜‘ํ•œ ํฌ์ธํ„ฐ { int *dumb = new

    int(10); /* ... */ } // <- LEAK! { auto smart = unique_ptr(new int(10)); /* ... */ } // <- RAII โ™ฅ
  12. C++ is not even close to memory safety โ€ข Dangling

    reference auto list = vector<int>(); /* ... */ auto& elem = list[0]; list.push_back(100); cout << elem; // BOOM!!
  13. Admit it C++ is not that good ์—ฌ์ „ํžˆ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹ ๋‹ค๋ฉด

    ์Šคํ†กํ™€๋ฆ„ ์ฆํ›„๊ตฐ์„ ์˜์‹ฌํ•ด๋ณด์‹ญ์‹œ์˜ค
  14. โ€œ์›๋ž˜ ๊ธฐ์กด์˜๊ฒƒ์„ ๋‚ซ๊ฒŒ ๋งŒ๋“œ๋Š”๊ฒŒ ์ƒˆ ์–ธ์–ด ๋งŒ๋“ค์–ด์„œ ํ•˜๋Š”๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ํž˜๋“ค์–ด.โ€

    ๋ฉ€ํ‹ฐ์ฝ”์–ด ์ปดํ“จํŒ… ์—ฐ๊ตฌ์‹ค ์ด์žฌ์ง„ ๊ต์ˆ˜๋‹˜ 5์›” 6์ผ 301๋™ 101ํ˜ธ ๋ฉ€ํ‹ฐ์ฝ”์–ด ์ˆ˜์—… ์‹œ๊ฐ„์ค‘ ๋ฐœ์ทŒ
  15. โ€œ์›๋ž˜ ๊ธฐ์กด์˜๊ฒƒ์„ ๋‚ซ๊ฒŒ ๋งŒ๋“œ๋Š”๊ฒŒ ์ƒˆ ์–ธ์–ด ๋งŒ๋“ค์–ด์„œ ํ•˜๋Š”๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ํž˜๋“ค์–ด.โ€

    ๋ฉ€ํ‹ฐ์ฝ”์–ด ์ปดํ“จํŒ… ์—ฐ๊ตฌ์‹ค ์ด์žฌ์ง„ ๊ต์ˆ˜๋‹˜ 5์›” 6์ผ 301๋™ 101ํ˜ธ ๋ฉ€ํ‹ฐ์ฝ”์–ด ์ˆ˜์—… ์‹œ๊ฐ„์ค‘ ๋ฐœ์ทŒ โ€œ๊ธฐ์กด์˜๊ฒƒ์„ ๋‚ซ๊ฒŒ ๋งŒ๋“œ๋А๋‹ˆ ์ƒˆ ์–ธ์–ด ๋งŒ๋“œ๋Š”๊ฒŒ ํ›จ์”ฌ ์‰ฌ์›Œ.โ€
  16. ์‰ฌ์›ก C++ โ€ข ์•”๊ฑธ๋ฆฌ๋Š” ๋ฌธ๋ฒ• โ€ข ๋ฐฐ์šธ๊ฒŒ ๊ฑ ๋งŽ์Œ โ€ข

    Makefile, autoconf, cmake, ninja, ... โ€ข ํ†ต์ผ๋œ ํŒจํ‚ค์ง€๋งค๋‹ˆ์ € X Rust โ€ข ์ Š์Œ โ€ข ๊ฐ„๋‹จํ•จ โ€ข cargo โ€ข cargo https://crates.io โ€ข ๋งคํฌ๋กœ, annotation, ...
  17. ๋””ํŽœ๋˜์‹œ ๊ด€๋ฆฌ โ€ข ๊ฑ pip, npm, gem ์–˜๋„ค๋ž‘ ๋‹ค๋ฅผ๊ฑฐ ์—†์Œ

    โ€ข ์ด๋Ÿฌ๋ฉด ์›ํ„ฐ์น˜๋กœ ๋‹ค ์„ค์น˜๋จ โ€ข ๋ฐ˜๋ฉด C++์—์„  ์ปดํŒŒ์ผ์˜ต์…˜ ๋ฐ”๊พธ๊ณ  ๊ฒฝ๋กœ์„ค์ •ํ•˜๊ณ  ๋ผ์ด ๋ธŒ๋Ÿฌ๋ฆฌ๋งˆ๋‹ค ์„ค์น˜๋ฐฉ๋ฒ• ๋‹ค ๋‹ค๋ฅด๊ณ  ๋‚œ๋ฆฌ๋ฅผ ์น˜์ง€ [dependencies] time = "*" glutin = "*"
  18. ๋””๋ฒ„๊น… โ€ข gdb, lldb, ์ž๊ธฐ๊ฐ€ ์›ํ•˜๋Š”๊ฑฐ ์“ฐ๋ฉด ๋จ โ€ข LLVM

    ๊ธฐ๋ฐ˜์ด๋‹ค๋ณด๋‹ˆ Xcode๋กœ๋„ ๋””๋ฒ„๊ทธํ•  ์ˆ˜ ์žˆ์Œ
  19. rust-kr.org โ€ข ํ•œ๋‹ฌ์— ํ•œ๋ฒˆ์”ฉ ๋ชจ์—ฌ์„œ ์ฝ”๋”ฉ๋ชจ์ž„์„ ํ•จ โ€ข ํ•™๊ต ๋ฐ–์—

    ๊ณ„์‹œ๋Š” ์ข‹์€ ๋ถ„๋“ค์„ ๋งŽ์ด ๋งŒ๋‚ ์ˆ˜ ์žˆ๋Š” ์•ˆ ํ”ํ•œ ๊ธฐํšŒ!
  20. ์ œ์•ˆ: UPnL ์—ฌ๋ฆ„ Rust ์Šคํ„ฐ๋”” โ€ข ๋จผ์ € Rust์— ๋Œ€ํ•ด ๋ฐฐ์šฐ๊ณ ,

    ๊ฐ์ž, ํ˜น์€ ๊ฐ™์ด ๋ญ”๊ฐ€๋ฅผ ๋งŒ๋“ค๋ฉด์„œ ์ง„ํ–‰ํ•˜๊ณ ์ž ํ•จ โ€ข ๊ธฐ๋Œ€ํšจ๊ณผ โ€ข ์‹œํ”„์— ์ต์ˆ™ํ•ด์ง€๋ฉด C/C++ ์ตํžˆ๋Š”๋ฐ์—๋„ ํ›จ์”ฌ ๋„์›€ ๋งŽ์ด ๋ ๊ฑฐ์—์—ผ โ€ข ๋œ ์ž‰์—ฌํ•œ ์—ฌ๋ฆ„๋ฐฉํ•™ โ€ข ์ƒˆ๋‚ด๊ธฐ์˜ ๊ฒฝ์šฐ ์„ ๋ฐฐ๋“ค์„ ๋œฏ์–ด๋จน์„ ์ˆ˜ ์žˆ์Œ โ€ข ํ•„์ฐธ์ž โ€ข sgkim โ€ข apple โ€ข kinetic
  21. ๊ทผํ™ฉ โ€ข Rust + OpenGL๋กœ 3D ๋Œ€์ „์•ก์…˜๊ฒŒ์ž„ ๋งŒ๋“œ๋Š”์ค‘ โ€ข ๋น ๋ฆ„

    + ํฌ๋กœ์Šคํ”Œ๋žซํผ โ€ข sgkim์ด๋ž‘ ๊ฐ™์ดํ•˜๋Š”์ค‘ โ€ข ๊ด€์‹ฌ์žˆ๋Š” ์ƒˆ๋‚ด๊ธฐ๋Š” ์—ฐ๋ฝํ•˜์‹œ์˜ค