Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, - - PowerPoint PPT Presentation

memory layout file i o
SMART_READER_LITE
LIVE PREVIEW

Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, - - PowerPoint PPT Presentation

Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C Outline Review HW1 (+command line arguments) Memory Layout File I/O HW1


slide-1
SLIDE 1

Memory ¡Layout, ¡File ¡I/O ¡

Bryce ¡Boe ¡ 2013/06/27 ¡ CS24, ¡Summer ¡2013 ¡C ¡

slide-2
SLIDE 2

Outline ¡

  • Review ¡HW1 ¡(+command ¡line ¡arguments) ¡
  • Memory ¡Layout ¡
  • File ¡I/O ¡
slide-3
SLIDE 3

HW1 ¡REVIEW ¡

slide-4
SLIDE 4

HW1 ¡Common ¡Problems ¡

  • Taking ¡input ¡from ¡stdin ¡(via ¡scanf) ¡
  • Performing ¡failure ¡tesPng ¡too ¡late ¡
  • Not ¡handling ¡the ¡0 ¡case ¡
  • Whitespace ¡issues ¡
  • Others? ¡
slide-5
SLIDE 5

HW1 ¡SoluPon ¡

  • <In-­‑class ¡review ¡of ¡hw1 ¡soluPon ¡source ¡code> ¡
slide-6
SLIDE 6

MEMORY ¡LAYOUT ¡

slide-7
SLIDE 7

View ¡from ¡three ¡Levels ¡

  • The ¡address ¡space ¡of ¡a ¡process ¡
  • FuncPon ¡acPvaPon ¡records ¡on ¡the ¡stack ¡
  • Data ¡within ¡an ¡acPvaPon ¡record ¡
slide-8
SLIDE 8

Simplified ¡process’s ¡address ¡space ¡

STACK ¡ HEAP ¡ CODE+ ¡

0xFFFFFFFF ¡ 0x00000000 ¡

slide-9
SLIDE 9

Data ¡Segments ¡

  • Code(+) ¡

– Program ¡code ¡ – IniPalizaPon ¡data, ¡global ¡and ¡staPc ¡variables ¡

  • Heap ¡

– Memory ¡chunks ¡allocated ¡via ¡malloc ¡

  • Stack ¡

– FuncPon ¡acPvaPon ¡records ¡

slide-10
SLIDE 10

CreaPng ¡and ¡destroying ¡acPvaPon ¡ records ¡

  • Program ¡starts ¡with ¡main ¡
  • main ¡calls ¡strsafeconcat ¡
  • strsafeconcat ¡calls ¡strlength ¡
  • strlength ¡returns ¡
  • strsafeconcat ¡calls ¡strlength ¡
  • strlength ¡returns ¡
  • strsafeconcat ¡returns ¡

main ¡ strsafeconcat ¡ strlength ¡ top ¡of ¡stack ¡ High ¡Memory ¡

slide-11
SLIDE 11

Simplified ¡funcPon ¡acPvaPon ¡records ¡

  • Stores ¡values ¡passed ¡into ¡the ¡funcPon ¡as ¡

parameters ¡

  • Stores ¡the ¡funcPon’s ¡local ¡variables ¡
slide-12
SLIDE 12

How ¡many ¡bytes ¡is ¡the ¡simplified ¡ acPvaPon ¡record? ¡

int ¡main(int ¡argc, ¡char ¡*argv[]) ¡{ ¡ ¡char ¡buf[8]; ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡argc; ¡++i) ¡ ¡ ¡buf[i] ¡= ¡argv[i][0]; ¡ } ¡

slide-13
SLIDE 13

main’s ¡simplified ¡acPvaPon ¡record ¡

argc ¡(int) ¡ 4 ¡bytes ¡ argv ¡(pointer) ¡ 4 ¡bytes ¡ buf ¡(char ¡array) ¡ 8 ¡bytes ¡ i ¡(int) ¡ 4 ¡bytes ¡ Total: ¡20 ¡bytes ¡ data ¡ stored ¡

slide-14
SLIDE 14

Think ¡about ¡it ¡

int ¡main() ¡{ ¡ ¡char ¡msg[] ¡= ¡“hello”; ¡ ¡char ¡buf[] ¡= ¡“1234567”; ¡ ¡char ¡msg2[] ¡= ¡“world”; ¡ } ¡

  • What ¡value ¡does ¡buf[8] ¡hold? ¡
  • What ¡about ¡msg[7]? ¡
slide-15
SLIDE 15

Similar ¡but ¡different ¡

int ¡main() ¡{ ¡ ¡int ¡x ¡= ¡0xDEADBEEF; ¡ ¡char ¡buf[] ¡= ¡“1234567”; ¡ } ¡

  • What ¡value ¡does ¡buf[8] ¡hold? ¡
slide-16
SLIDE 16

InspecPng ¡addresses ¡in ¡a ¡program ¡

  • <In ¡class ¡review ¡of ¡variables_in_memory.c> ¡
slide-17
SLIDE 17

FILE ¡I/O ¡

slide-18
SLIDE 18

File ¡I/O ¡

  • I/O ¡stands ¡for ¡input/output ¡
  • Provided ¡by ¡the ¡stdio ¡library ¡(stdio.h) ¡
  • Allows ¡reading ¡and ¡wriPng ¡to/from ¡streams ¡

(type: ¡FILE ¡*) ¡

– stdin ¡(read ¡only) ¡ – stdout ¡/ ¡stderr ¡(write ¡only) ¡ – named ¡files ¡

slide-19
SLIDE 19

stdio.h ¡

  • Explore ¡via ¡opengroup: ¡hop://

pubs.opengroup.org/onlinepubs/009695399/ basedefs/stdio.h.html ¡

  • Explore ¡via ¡`man ¡stdio.h` ¡(same ¡info ¡as ¡
  • pengroup, ¡but ¡not ¡easily ¡searchable ¡or ¡

linkable) ¡

slide-20
SLIDE 20

Opening ¡named ¡files ¡and ¡closing ¡ streams ¡

  • FILE* ¡fopen(char ¡*filename, ¡char ¡*mode) ¡

– ¡open ¡a ¡file ¡specifying ¡whether ¡to ¡open ¡for ¡ reading, ¡wriPng, ¡appending, ¡ ¡and ¡create ¡or ¡ truncate ¡if ¡desired ¡

  • int ¡fclose(FILE ¡*stream) ¡– ¡close ¡an ¡open ¡FILE* ¡

– Flush ¡and ¡close ¡the ¡stream ¡ – Return ¡0 ¡on ¡success ¡

slide-21
SLIDE 21

Reading ¡from ¡streams ¡

  • int ¡fgetc(FILE ¡*stream) ¡

– Returns ¡the ¡next ¡character ¡in ¡the ¡stream ¡or ¡EOF ¡ (this ¡is ¡why ¡it ¡returns ¡an ¡int) ¡

  • char ¡*fgets(char ¡*buf, ¡int ¡n, ¡FILE ¡*stream) ¡

– Read ¡at ¡most ¡n-­‑1 ¡bytes ¡from ¡stream ¡into ¡buf ¡ – Also ¡stops ¡when ¡‘\n’ ¡or ¡EOF ¡is ¡reached ¡ – Terminates ¡buf ¡with ¡the ¡null ¡character ¡‘\0’ ¡

slide-22
SLIDE 22

Other ¡read ¡funcPons ¡

  • fread ¡– ¡very ¡useful ¡(especially ¡when ¡input ¡is ¡

unbounded), ¡we ¡won’t ¡use ¡in ¡this ¡class ¡

  • fscanf ¡– ¡useful, ¡but ¡tricky, ¡don’t ¡use ¡in ¡this ¡

class ¡

  • FuncPons ¡with ¡similar ¡names ¡less ¡the ¡‘f’ ¡

– Uses ¡the ¡stdin ¡stream ¡thus ¡doesn’t ¡require ¡the ¡ stream ¡argument ¡

slide-23
SLIDE 23

WriPng ¡to ¡streams ¡

  • int ¡fputc(int ¡c, ¡FILE ¡*stream) ¡

– Write ¡a ¡single ¡character ¡c ¡to ¡stream ¡

  • int ¡fputs(char ¡*buf, ¡FILE ¡*stream) ¡

– Write ¡the ¡null-­‑terminated ¡string ¡in ¡buf ¡to ¡stream ¡

  • int ¡fprinw(FILE ¡*stream, ¡char ¡*format, ¡…) ¡

– Write ¡formaoed ¡string ¡to ¡stream ¡making ¡the ¡ specified ¡replacements ¡

slide-24
SLIDE 24

SECURITY ¡WARNING ¡

  • NEVER ¡do ¡the ¡following: ¡

fprinw(stdout, ¡buf); ¡ ¡// ¡buf ¡is ¡some ¡c-­‑string ¡

  • Could ¡allow ¡an ¡aoacker ¡to ¡inspect ¡and ¡change ¡

your ¡program ¡(format ¡string ¡exploit) ¡

  • Use ¡either ¡fputs ¡or ¡fprin-(stdout, ¡“%s”, ¡buf) ¡
slide-25
SLIDE 25

Other ¡write ¡funcPons ¡

  • fwrite ¡– ¡generally ¡very ¡useful, ¡we ¡won’t ¡use ¡in ¡

this ¡class ¡

  • FuncPons ¡with ¡similar ¡names ¡less ¡the ¡‘f’ ¡

– Uses ¡the ¡stdout ¡stream ¡thus ¡doesn’t ¡require ¡the ¡ stream ¡argument ¡

slide-26
SLIDE 26

Other ¡useful ¡stream ¡funcPons ¡

  • int ¡feof(FILE ¡*stream) ¡

– Return ¡non-­‑zero ¡if ¡the ¡stream ¡has ¡reached ¡the ¡ end ¡of ¡the ¡file ¡

  • int ¡fflush(FILE ¡*stream) ¡

– Force ¡wriPng ¡any ¡buffered ¡data ¡to ¡the ¡stream ¡ – Flushing ¡typically ¡occurs ¡when ¡a ¡newline ¡is ¡ encountered, ¡thus ¡fflush ¡is ¡o|en ¡needed ¡when ¡ newlines ¡aren’t ¡used ¡

slide-27
SLIDE 27

I/O ¡QuesPons ¡

  • Why ¡does ¡fgetc/fputc ¡return/get ¡an ¡integer? ¡
  • If ¡a ¡file ¡with ¡only ¡a ¡single ¡newline ¡at ¡the ¡end ¡is ¡

32 ¡bytes ¡long, ¡how ¡many ¡bytes ¡does ¡the ¡ buffer ¡for ¡fgets ¡require ¡to ¡read ¡the ¡enPre ¡file? ¡

slide-28
SLIDE 28

More ¡I/O ¡QuesPons ¡

  • When ¡using ¡fgets, ¡how ¡can ¡you ¡determine ¡if ¡

the ¡string ¡is ¡longer ¡than ¡the ¡value ¡of ¡‘n’ ¡(the ¡ number ¡of ¡bytes ¡to ¡read) ¡

  • What ¡will ¡happen ¡if ¡the ¡‘n’ ¡parameter ¡to ¡fgets ¡

is ¡larger ¡than ¡the ¡buffer? ¡

slide-29
SLIDE 29

Real-­‑Pme ¡cat ¡program ¡wriPng ¡

  • <In ¡class ¡creaPon ¡of ¡simple_copy.c> ¡
slide-30
SLIDE 30

For ¡Tuesday ¡

  • Finish ¡reading ¡chapter ¡1 ¡in ¡the ¡text ¡book ¡