-
Notifications
You must be signed in to change notification settings - Fork 41
/
stdio.k
2742 lines (2519 loc) · 123 KB
/
stdio.k
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module LIBC-STDIO
imports C-CONFIGURATION
imports INT
imports K-IO
imports STRING
imports BITS-SYNTAX
imports COMPAT-SYNTAX
imports C-BITSIZE-SYNTAX
imports C-COMMON-PROMOTION-SYNTAX
imports C-CONVERSION-SYNTAX
imports C-DYNAMIC-SYNTAX
imports C-ERROR-SYNTAX
imports C-EXECUTION-ERROR-SYNTAX
imports C-IO-SYNTAX
imports C-MEMORY-ALLOC-SYNTAX
imports C-MEMORY-READING-SYNTAX
imports C-MEMORY-WRITING-SYNTAX
imports C-NATIVE-BRIDGE-SYNTAX
imports C-OS-SETTINGS-SYNTAX
imports C-SETTINGS-SYNTAX
imports C-SYMLOC-SYNTAX
imports C-SYNTAX
imports C-TYPING-SYNTAX
imports DELETE-OBJECT-SYNTAX
imports LIBC-BUILTIN-SYNTAX
imports LIBC-IO-SYNTAX
imports LIBC-STDLIB-SYNTAX
imports LIBC-TYPES-SYNTAX
rule builtin("rewind", FDPtr::RValue)
=> fseek((* FDPtr), 0, 0) ~> discard
~> clearerr(* FDPtr)
[structural]
rule builtin("fseek", FDPtr::RValue, tv(Offset:Int, _), tv(Whence:Int, _))
=> fseek((* FDPtr), Offset, Whence)
[structural]
rule builtin("fsetpos", FDPtr::RValue, PosPtr::RValue)
=> fsetpos((* FDPtr), (* PosPtr))
[structural]
rule builtin("fflush", FDPtr::RValue)
=> fflush(* FDPtr)
requires notBool isNull(FDPtr)
[structural]
rule builtin("fflush", FDPtr::RValue)
=> flushAll ~> success
requires isNull(FDPtr)
[structural]
syntax KItem ::= fsetpos(K, K)
context fsetpos(_, HOLE:KItem => reval(HOLE)) [result(RValue)]
context fsetpos(HOLE:KItem => reval(HOLE), _) [result(RValue)]
rule <k> fsetpos(tv(opaque(FD:FD, _), _) #as V::RValue, tv(opaque(fpos(Path::String, Pos::Int), _), _))
=> fseek(V, Pos, 0)
...</k>
<fid> FD </fid>
<uri> Path </uri>
[structural]
rule <k> fsetpos(V:RValue, _) => setErrno(#EBADF) ~> eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool (notBool getFD(V) in_keys(OpenFiles))
[structural]
rule <k> (.K => UNDEF("STDIO9", "'fsetpos': called to set a position that was not returned by a previous successful call to the fgetpos function on a stream associated with the same file."))
~> fsetpos(tv(opaque(FD:FD, _), _), tv(V::CValue, _))
...</k>
<fid> FD </fid>
<uri> Path':String </uri>
requires fposPath(V) =/=String Path'
[structural]
syntax FauxFD ::= getFD(RValue) [function]
rule getFD(tv(opaque(FD:FD, _), _)) => FD
rule getFD(_) => "" [owise]
syntax String ::= fposPath(CValue) [function]
rule fposPath(opaque(fpos(Path::String, _), _)) => Path
rule fposPath(_) => "" [owise]
syntax KItem ::= fflush(K)
context fflush(HOLE:KItem => reval(HOLE)) [result(RValue)]
rule <k> fflush(tv(opaque(FD:Int, _), _))
=> flush(FD)
~> success
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
<last-op> Op:LastFileOp => flushOp </last-op>
requires isWritable(Mode) andBool (Op =/=K inputOp)
[structural]
rule <k> fflush(V:RValue) => setErrno(#EBADF) ~> eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool (notBool getFD(V) in_keys(OpenFiles))
rule <k> (.K => UNDEF("STDIO7", "'fflush': called on an input stream or an input/output stream on which the most recent operation was input."))
~> fflush(tv(opaque(FD:Int, _), _))
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
<last-op> Op:LastFileOp </last-op>
requires notBool (isWritable(Mode) andBool (Op =/=K inputOp))
[structural]
syntax KItem ::= fseek(K, Int, Int)
context fseek(HOLE:KItem => reval(HOLE), _, _) [result(RValue)]
rule fseek(tv(opaque(FD:Int, _), _), Offset::Int, 0)
=> flush(FD)
~> seek(FD, Offset, seekSet)
~> cleareof(FD)
~> success
[structural]
rule fseek(tv(opaque(FD:Int, _), _), Offset::Int, 1)
=> flush(FD)
~> seek(FD, Offset, seekCur)
~> cleareof(FD)
~> success
[structural]
rule fseek(tv(opaque(FD:Int, _), _), Offset::Int, 2)
=> flush(FD)
~> seek(FD, Offset, seekEnd)
~> cleareof(FD)
~> success
[structural]
rule builtin("ftell", FDPtr::RValue)
=> ftell(* FDPtr)
[structural]
rule builtin("fgetpos", FDPtr::RValue, PosPtr::RValue)
=> fgetpos((* FDPtr), PosPtr)
[structural]
syntax KItem ::= ftell(K)
context ftell(HOLE:KItem => reval(HOLE)) [result(RValue)]
rule (.K => tell(FD))
~> ftell(tv(opaque(FD:Int, _), _))
[structural]
rule tv(Pos:Int, _) ~> ftell(_)
=> tv(Pos, utype(long-int))
[structural]
syntax KItem ::= fgetpos(K, RValue)
| fpos(String, Int)
context fgetpos(HOLE:KItem => reval(HOLE), _) [result(RValue)]
rule <k> (.K => tell(FD))
~> fgetpos(tv(opaque(FD:Int, _), _), _)
...</k>
<open-files> OpenFiles:Map </open-files>
requires FD in_keys(OpenFiles)
[structural]
rule <k> fgetpos(V:RValue, _) => setErrno(#EBADF) ~> eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool (notBool getFD(V) in_keys(OpenFiles))
[structural]
rule <k> tv(Pos:Int, _) ~> fgetpos(tv(opaque(FD:Int, _), _), PosPtr::RValue)
=> Computation((* PosPtr) := tv(opaque(fpos(Path, Pos), utype(fpos_t)), utype(fpos_t)))
~> success
...</k>
<fid> FD </fid>
<uri> Path:String </uri>
[structural]
rule builtin("fputc", tv(N:Int, _), FDPtr::RValue)
=> fputc(N, (* FDPtr))
[structural]
rule builtin("_IO_putc" => "putc", _, _)
[structural]
rule builtin("putc", tv(N:Int, _), FDPtr::RValue)
=> fputc(N, (* FDPtr))
[structural]
rule builtin("putchar", tv(N:Int, _))
=> fputc(N, stdout)
[structural]
syntax KItem ::= fputc(Int, K)
context fputc(_, HOLE:KItem => reval(HOLE)) [result(RValue)]
rule <k> fputc(N::Int, tv(opaque(FD:Int, _), _))
=> writeFD(FD, N)
~> tv(N, utype(int))
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
requires isWritable(Mode)
[structural]
rule <k> fputc(_, V:RValue) => eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool (notBool getFD(V) in_keys(OpenFiles))
[structural]
rule <k> fputc(N::Int, tv(opaque(FD:Int, _), _))
=> eof
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
<ferror> _ => getErrno(#EBADF) </ferror>
requires notBool isWritable(Mode)
[structural]
rule builtin("fgetc", FDPtr::RValue)
=> fgetc(* FDPtr)
[structural]
rule builtin("_IO_getc" => "getc", _)
[structural]
rule builtin("getc", FDPtr::RValue)
=> fgetc(* FDPtr)
[structural]
rule builtin("getchar")
=> fgetc(stdin)
[structural]
rule builtin("ungetc", tv(C:Int, _), FDPtr::RValue)
=> ungetc(C, (* FDPtr))
requires C =/=K value(eof)
[structural]
rule builtin("ungetc", tv(C:Int, _), _)
=> eof
requires C ==K value(eof)
[structural]
syntax KItem ::= fgetc(K)
context fgetc(HOLE:KItem => reval(HOLE)) [result(RValue)]
rule <k> (.K => readFD(FD, 1))
~> fgetc(tv(opaque(FD:Int, _), _))
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
requires isReadable(Mode)
[structural]
rule <k> fgetc(V:RValue) => eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool (notBool getFD(V) in_keys(OpenFiles))
[structural]
rule <k> fgetc(tv(opaque(FD:Int, _), _))
=> eof
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
<ferror> _ => getErrno(#EBADF) </ferror>
requires notBool isReadable(Mode)
[structural]
rule str(S::String) ~> fgetc(_)
=> tv(ordChar(firstChar(S)), utype(int))
requires lengthString(S) >Int 0
[structural]
rule str("") ~> fgetc(_) => eof
[structural]
syntax KItem ::= ungetc(Int, K)
context ungetc(_, HOLE:KItem => reval(HOLE)) [result(RValue)]
rule <k> ungetc(C::Int, tv(opaque(FD:Int, _), _))
=> unget(FD, chrChar(C))
~> cleareof(FD)
~> tv(C, utype(int))
...</k>
<open-files> OpenFiles:Map </open-files>
requires FD in_keys(OpenFiles)
[structural]
rule <k> ungetc(_, V:RValue) => eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool (notBool getFD(V) in_keys(OpenFiles))
[structural]
rule builtin("feof", FDPtr::RValue) => feof(* FDPtr)
[structural]
rule builtin("ferror", FDPtr::RValue) => ferror(* FDPtr)
[structural]
rule builtin("fileno", FDPtr::RValue) => fileno(* FDPtr)
[structural]
syntax KItem ::= feof(K)
context feof(HOLE:KItem => reval(HOLE)) [result(RValue)]
rule <k> feof(tv(opaque(FD:Int, _), _)) => tv(1, utype(int)) ...</k>
<fid> FD </fid>
<read-buffer> "" </read-buffer>
<feof> true </feof>
[structural]
rule <k> feof(tv(opaque(FD:Int, _), _)) => tv(0, utype(int)) ...</k>
<fid> FD </fid>
<read-buffer> Buffer::String </read-buffer>
<feof> Eof:Bool </feof>
requires notBool (Buffer ==K "" andBool Eof)
[structural]
rule <k> feof(V:RValue) => tv(0, utype(int)) ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool (notBool getFD(V) in_keys(OpenFiles))
[structural]
syntax KItem ::= ferror(K)
context ferror(HOLE:KItem => reval(HOLE)) [result(RValue)]
rule <k> ferror(tv(opaque(FD:Int, _), _)) => tv(1, utype(int)) ...</k>
<fid> FD </fid>
<ferror> Err:Int </ferror>
requires Err =/=Int 0
[structural]
rule <k> ferror(tv(opaque(FD:Int, _), _)) => tv(0, utype(int)) ...</k>
<fid> FD </fid>
<ferror> 0 </ferror>
[structural]
rule <k> ferror(V:RValue) => tv(0, utype(int)) ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool (notBool getFD(V) in_keys(OpenFiles))
[structural]
syntax KItem ::= fileno(K)
context fileno(HOLE:KItem => reval(HOLE)) [result(RValue)]
rule <k> fileno(tv(opaque(FD:Int, _), _)) => tv(FD, utype(int)) ...</k>
<open-files> OpenFiles:Map </open-files>
requires FD in_keys(OpenFiles)
[structural]
rule <k> fileno(V:RValue) => setErrno(#EBADF) ~> eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool notBool (getFD(V) in_keys(OpenFiles))
[structural]
rule builtin("clearerr", FDPtr::RValue)
=> clearerr(* FDPtr)
[structural]
syntax KItem ::= clearerr(K)
context clearerr(HOLE:KItem => reval(HOLE)) [result(RValue)]
rule <k> clearerr(tv(opaque(FD:Int, _), _))
=> cleareof(FD) ~> voidVal
...</k>
<fid> FD </fid>
<ferror> _ => 0 </ferror>
[structural]
rule <k> clearerr(V:RValue) => voidVal ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool notBool (getFD(V) in_keys(OpenFiles))
[structural]
syntax KItem ::= cleareof(Int)
rule <k> cleareof(FD::Int) => .K ...</k>
<fid> FD </fid>
<feof> _ => false </feof>
[structural]
rule builtin("fclose", FDPtr::RValue) => fclose(* FDPtr, FDPtr)
requires notBool isNull(FDPtr)
[structural]
rule builtin("fclose", FDPtr::RValue) => eof
requires isNull(FDPtr)
[structural]
syntax KItem ::= fclose(K, RValue)
| "fclose'" "(" Int ")"
context fclose(HOLE:KItem => reval(HOLE), _) [result(RValue)]
rule fclose(tv(opaque(FD:Int, _), _), tv(Loc::SymLoc, _))
=> deleteObject(base(Loc))
~> fclose'(FD)
[structural]
rule <k> fclose'(FD::Int)
=> flush(FD)
~> close(FD)
~> success
...</k>
<open-files> OpenFiles:Map </open-files>
requires FD in_keys(OpenFiles)
[structural]
rule <k> fclose'(FD::Int) => eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires notBool (FD in_keys(OpenFiles))
[structural]
rule builtin("fopen", Filename::RValue, Mode::RValue)
=> alloc(bnew(!FDLoc:Int, file, libc), file, byteSizeofType(type(file)))
~> fopen(bnew(!FDLoc, file, libc), getString(Filename), getString(Mode))
[structural]
rule builtin("freopen", Filename::RValue, Mode::RValue, tv(Loc:SymLoc, T::UType))
=> freopen(Filename, Mode, base(Loc), (* tv(stripProv(Loc), T)))
requires notBool isNull(tv(Loc, T))
[structural]
rule builtin("freopen", _, _, FDPtr::RValue)
=> tv(NullPointer, utype(pointerType(type(file))))
requires isNull(FDPtr)
[structural]
rule builtin("tmpnam", S::RValue)
=> nativeCall("tmpnam", ListItem(S), .List)
[structural]
rule (.K => Call(Identifier("tmpnam"),
list(ListItem(tv(0, utype(pointerType(type(char))))))))
~> builtin("tmpfile")
[structural]
// TODO(chathhorn): delete on close and program exit?
rule S:RValue ~> builtin("tmpfile")
=> alloc(bnew(!FDLoc:Int, file, libc), file, byteSizeofType(type(file)))
~> fopen(bnew(!FDLoc, file, libc), getString(S), str("wb+"))
[structural]
// TODO(chathhorn): need another level of indirection here so e.g.
// stdin/out can be redirected.
syntax KItem ::= freopen(RValue, RValue, SymBase, K)
context freopen(_, _, _, HOLE:KItem => reval(HOLE)) [result(RValue)]
rule freopen(Filename::RValue, Mode::RValue, Base:SymBase, tv(opaque(FD:Int, T::UType), T'::UType))
=> fclose'(FD) ~> discard
~> fopen(Base, getString(Filename), getString(Mode))
requires notBool isNull(Filename)
[structural]
rule <k> freopen(Filename::RValue, Mode::RValue, Base:SymBase, tv(opaque(FD:Int, T::UType), T'::UType))
=> fclose'(FD) ~> discard
~> fopen(Base, Filename', getString(Mode))
...</k>
<fid> FD </fid>
<uri> Filename':String </uri>
requires isNull(Filename)
[structural]
syntax KItem ::= fopen(SymBase, KItem, KItem) [strict(2,3)]
rule fopen(Base::SymBase, str(Filename::String), str(Mode::String))
=> fopen'(Base, Filename, Mode, #open(Filename, Mode))
requires isReadable(Mode) orBool isWritable(Mode) orBool isReadWritable(Mode)
[structural]
rule (.K => UNDEF("STDIO8", "'fopen' or 'freopen': invalid open mode \"" +String Mode +String "\"."))
~> fopen(_, _, str(Mode::String))
requires notBool (isReadable(Mode) orBool isWritable(Mode) orBool isReadWritable(Mode))
[structural]
syntax KItem ::= "fopen'" "(" SymBase "," String "," String "," K ")"
rule <k> fopen'(Base::SymBase, Filename::String, Mode::String, FD:Int)
=> Computation(fileLVal(Base) := tv(opaque(FD, utype(file)), utype(file)))
~> & fileLVal(Base)
...</k>
<open-files>... .Map => FD |-> printStackTrace(L, Tu, Sc, Loc, Env) </open-files>
<call-stack> L:List </call-stack>
<curr-tu> Tu::String </curr-tu>
<renv> Env::Map </renv>
<curr-program-loc> Loc:CabsLoc </curr-program-loc>
<curr-scope> Sc:Scope </curr-scope>
<files>...
(.Bag =>
<file>...
<fid> FD </fid>
<uri> Filename </uri>
<mode> Mode </mode>
...</file>
)
...</files>
[structural]
rule <k> fopen'(_, _, _, Err:IOError)
=> setErrno(Err)
~> tv(NullPointer, utype(pointerType(type(file))))
...</k>
rule builtin("setbuf", FDPtr::RValue, tv(Loc:SymLoc, _) #as BufPtr::RValue)
=> trapBuf(Loc, bufSiz)
~> setbuf((* FDPtr), fullBuf, bufSiz) ~> discard
~> voidVal
requires notBool isNull(BufPtr)
[structural]
rule builtin("setbuf", FDPtr::RValue, BufPtr::RValue)
=> setbuf((* FDPtr), noBuf, bufSiz) ~> discard
~> voidVal
requires isNull(BufPtr)
[structural]
rule builtin("setbuffer", FDPtr::RValue, tv(Loc:SymLoc, _) #as BufPtr::RValue, tv(Sz:Int, _))
=> trapBuf(Loc, Sz)
~> setbuf((* FDPtr), fullBuf, Sz) ~> discard
~> voidVal
requires notBool isNull(BufPtr)
[structural]
rule builtin("setbuffer", FDPtr::RValue, BufPtr::RValue, tv(Sz:Int, _))
=> setbuf((* FDPtr), noBuf, Sz) ~> discard
~> voidVal
requires isNull(BufPtr)
[structural]
rule builtin("setvbuf", FDPtr::RValue, tv(Loc:SymLoc, _), tv(0, _), tv(Sz:Int, _))
=> trapBuf(Loc, Sz)
~> setbuf((* FDPtr), fullBuf, Sz)
[structural]
rule builtin("setvbuf", FDPtr::RValue, tv(Loc:SymLoc, _), tv(1, _), tv(Sz:Int, _))
=> trapBuf(Loc, Sz)
~> setbuf((* FDPtr), lineBuf, Sz)
[structural]
rule builtin("setvbuf", FDPtr::RValue, tv(Loc:SymLoc, _), tv(2, _), tv(Sz:Int, _))
=> trapBuf(Loc, Sz)
~> setbuf((* FDPtr), noBuf, Sz)
[structural]
rule builtin("setlinebuf", FDPtr::RValue)
=> setbuf((* FDPtr), lineBuf, bufSiz) ~> discard
~> voidVal
[structural]
syntax KItem ::= trapBuf(SymLoc, Int)
rule trapBuf(Loc::SymLoc, Sz::Int)
=> initBytes(stripProv(Loc), times(Sz, piece(trap, cfg:bitsPerByte)))
requires notBool isNull(tv(Loc, utype(pointerType(type(void)))))
andBool Sz >Int 0
rule trapBuf(_, _) => .K [owise]
syntax KItem ::= setbuf(K, BufMode, Int)
context setbuf(HOLE:KItem => reval(HOLE), _, _) [result(RValue)]
rule <k> setbuf(tv(opaque(FD:Int, _), _), Mode::BufMode, Sz::Int)
=> setBuf(FD, Mode, Sz)
~> success
...</k>
<open-files> OpenFiles:Map </open-files>
requires FD in_keys(OpenFiles)
[structural]
rule <k> setbuf(V:RValue, _, _) => setErrno(#EBADF) ~> eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool notBool (getFD(V) in_keys(OpenFiles))
[structural]
rule builtin("fread", Ptr::RValue, tv(Size:Int, _), tv(NMemb:Int, _), FDPtr::RValue)
=> fread(Ptr, Size, NMemb, (* FDPtr))
[structural]
rule (.K => fscanf(stdin, str("%[^\n]"), ListItem(Ptr)))
~> builtin("gets", Ptr::RValue)
[structural]
rule tv(1, _) ~> builtin("gets", Ptr::RValue) => Ptr
[structural]
rule tv(N::CValue, _) ~> builtin("gets", Ptr::RValue) => tv(NullPointer, utype(pointerType(type(char))))
requires N =/=K 1
[structural]
rule builtin("fgets", Ptr::RValue, tv(NMemb:Int, _), FDPtr::RValue)
=> fgets(Ptr, NMemb, (* FDPtr))
[structural]
syntax KItem ::= fread(RValue, Int, Int, K)
context fread(_, _, _, HOLE:KItem => reval(HOLE)) [result(RValue)]
rule <k> fread(tv(Loc:SymLoc, _), Size::Int, NMemb::Int, tv(opaque(FD:Int, _), _))
=> fread'(Loc, Size, readFD(FD, Size *Int NMemb))
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
requires isReadable(Mode)
[structural]
rule <k> fread(_, _, _, tv(opaque(FD:Int, _), _))
=> eof
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
<ferror> _ => getErrno(#EBADF) </ferror>
requires notBool isReadable(Mode)
[structural]
rule <k> fread(_, _, _, V:RValue) => eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool notBool (getFD(V) in_keys(OpenFiles))
[structural]
syntax KItem ::= "fread'" "(" SymLoc "," Int "," KItem ")" [strict(3)]
rule fread'(Loc:SymLoc, Size::Int, str(Read::String))
=> writeChars(Loc, Read)
~> trapBuf(Loc +bytes lengthString(Read), Size -Int (lengthString(Read) %Int Size))
~> tv(lengthString(Read) /Int Size, utype(size_t))
requires (lengthString(Read) %Int Size) =/=Int 0
[structural]
rule fread'(Loc:SymLoc, Size::Int, str(Read::String))
=> writeChars(Loc, Read)
~> tv(lengthString(Read) /Int Size, utype(size_t))
requires (lengthString(Read) %Int Size) ==Int 0
[structural]
syntax KItem ::= fgets(RValue, Int, K)
context fgets(_, _, HOLE:KItem => reval(HOLE)) [result(RValue)]
rule fgets(Ptr::RValue, NMemb::Int, _) => Ptr
requires NMemb <Int 1
[structural]
rule fgets(Ptr::RValue, 1, _)
=> Computation((* Ptr) := tv(0, utype(innerType(type(Ptr)))))
~> Ptr
[structural]
rule (.K => fscanf(FD, str("%" +String showInt(NMemb -Int 1) +String "[^\n]"), ListItem(Ptr)))
~> fgets(Ptr::RValue, NMemb::Int, FD:RValue)
requires NMemb >Int 1
[structural]
rule <k> V:RValue ~> fgets(tv(Loc:SymLoc, _), NMemb::Int, tv(opaque(FD::Int, _), _))
=> trapBuf(Loc, NMemb)
~> tv(NullPointer, utype(pointerType(type(char))))
...</k>
<fid> FD </fid>
<read-buffer> Buffer::String </read-buffer>
<feof> Eof:Bool </feof>
requires V ==K eof andBool notBool (Buffer ==K "" andBool Eof)
[structural]
rule <k> tv(V:Int, _) ~> fgets(tv(Loc:SymLoc, _), NMemb::Int, tv(opaque(FD::Int, _), _))
=> trapBuf(Loc, NMemb)
~> tv(NullPointer, utype(pointerType(type(char))))
...</k>
<fid> FD </fid>
<read-buffer> "" </read-buffer>
<feof> true </feof>
requires V =/=Int 1
[structural]
rule <k> (tv(0, _) => str("")) ~> fgets(_, _, tv(opaque(FD::Int, _), _)) ...</k>
<fid> FD </fid>
<read-buffer> Buffer::String </read-buffer>
<feof> Eof:Bool </feof>
requires notBool (Buffer ==K "" andBool Eof)
[structural]
rule (tv(1, _) => getString(Ptr)) ~> fgets(Ptr::RValue, _, _)
[structural]
rule str(S::String) ~> fgets(Ptr::RValue, NMemb::Int, _)
=> Ptr
requires lengthString(S) >=Int NMemb -Int 1
[structural]
rule (str(S::String) => readNewline(Ptr + lengthString(S), FD)) ~> fgets(Ptr::RValue, NMemb::Int, FD:RValue)
requires lengthString(S) <Int NMemb -Int 1
[structural]
rule doneReadNewline ~> fgets(Ptr::RValue, NMemb::Int, _) => Ptr
[structural]
syntax KItem ::= readNewline(K, RValue)
syntax KResult ::= "doneReadNewline"
context readNewline(HOLE:KItem => reval(HOLE), _) [result(RValue)]
rule (.K => fscanf(FD, str("%1[\n]"), ListItem(Ptr)))
~> readNewline(Ptr:RValue, FD::RValue)
[structural]
rule _:RValue ~> readNewline(_, _) => doneReadNewline
[structural]
//////////////////////////
// C1X Here we might want to assume different threads cannot interleave
// wrt printf, based on 7.1.4:5. However, 7.1.4:4 seems to say exactly
// the opposite.
// on second thought, it probably is saying that while the shared data is
// protected against races, it can still be interleaved
// fixme despite the above, still need to handle interleaving of user data
// read during evaluation of printf. should use something like a buffer
// to do this
rule [printf]:
builtin*("printf", Format::RValue, VarArgs:List)
=> fprintf(stdout, formatter(getString(Format), VarArgs))
[structural]
rule [fprintf]:
builtin*("fprintf", FDPtr::RValue, Format::RValue, VarArgs:List)
=> fprintf((* FDPtr), formatter(getString(Format), VarArgs))
[structural]
rule [fputs]:
builtin("fputs", Str::RValue, FDPtr::RValue)
=> fprintf(* FDPtr, formatter(str("%s"), ListItem(Str)))
[structural]
rule [puts]:
builtin("puts", Str::RValue)
=> fprintf(stdout, formatter(str("%s\n"), ListItem(Str)))
[structural]
rule builtin("fwrite", Ptr::RValue, tv(Size:Int, _), tv(NMemb:Int, _), FDPtr::RValue)
=> fwrite(getBytes(Ptr, Size *Int NMemb), Size, (* FDPtr))
[structural]
syntax KItem ::= fprintf(K, KItem) [strict(2)]
context fprintf(HOLE:KItem => reval(HOLE), _) [result(RValue)]
rule <k> fprintf(tv(opaque(FD:Int, _), _), formattedResult(S:String))
=> writeFD(FD, S)
~> tv(lengthString(S), utype(int))
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
requires isWritable(Mode)
[structural]
rule <k> fprintf(tv(opaque(FD:Int, _), _), formattedResult(S:String))
=> eof
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
<ferror> _ => getErrno(#EBADF) </ferror>
requires notBool isWritable(Mode)
[structural]
rule <k> fprintf(V:RValue, _) => eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool notBool (getFD(V) in_keys(OpenFiles))
[structural]
syntax KItem ::= fwrite(KItem, Int, K) [strict(1)]
context fwrite(_, _, HOLE:KItem => reval(HOLE)) [result(RValue)]
rule <k> fwrite(str(S::String), Size::Int, tv(opaque(FD:Int, _), _))
=> writeFD(FD, S)
~> tv(lengthString(S) /Int Size, utype(size_t))
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
requires isWritable(Mode)
[structural]
rule <k> fwrite(str(S::String), Size::Int, tv(opaque(FD:Int, _), _))
=> eof
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
<ferror> _ => getErrno(#EBADF) </ferror>
requires notBool isWritable(Mode)
[structural]
rule <k> fwrite(_, _, V:RValue) => eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool notBool (getFD(V) in_keys(OpenFiles))
[structural]
syntax List ::= derefList(List) [function]
rule derefList(.List) => .List
rule derefList(ListItem(X:KItem) L::List) => ListItem(* X) derefList(L)
rule [vprintf]:
<k> (.K => toHeatList(derefList(VarArgs)))
~> builtin("vprintf", _, tv(opaque(VaList::Int, _), _))
...</k>
<va-lists>... VaList |-> VarArgs:List ...</va-lists>
[structural]
rule (.K => errorUninitVaList)
~> builtin("vprintf", _, tv(V::CValue, _))
requires opaque(...) :/=K V
[structural]
rule <k> (.K => errorUninitVaList)
~> builtin("vprintf", _, tv(opaque(VaList::Int, _), _))
...</k>
<va-lists> VaLists:Map </va-lists>
requires notBool VaList in_keys(VaLists)
[structural]
rule H:HeatList ~> builtin("vprintf", Format::RValue, _)
=> fprintf(stdout, formatter(getString(Format), hListToList(H)))
requires isKResult(H)
[structural]
syntax KItem ::= "errorUninitVaList" [function]
rule errorUninitVaList => UNDEF("STDIO3", "'v*printf': uninitialized va_list argument.")
rule [vfprintf]:
<k> (.K => toHeatList(derefList(VarArgs)))
~> builtin("vfprintf", _, _, tv(opaque(VaList::Int, _), _))
...</k>
<va-lists>... VaList |-> VarArgs:List ...</va-lists>
[structural]
rule (.K => errorUninitVaList)
~> builtin("vfprintf", _, _, tv(V::CValue, _))
requires opaque(...) :/=K V
[structural]
rule <k> (.K => errorUninitVaList)
~> builtin("vfprintf", _, _, tv(opaque(VaList::Int, _), _))
...</k>
<va-lists> VaLists:Map </va-lists>
requires notBool VaList in_keys(VaLists)
[structural]
rule H:HeatList ~> builtin("vfprintf", FDPtr::RValue, Format::RValue, _)
=> fprintf((* FDPtr), formatter(getString(Format), hListToList(H)))
requires isKResult(H)
[structural]
rule builtin*("sprintf", Dest::RValue, Format::RValue, VarArgs:List)
=> sprintf(formatter(getString(Format), VarArgs), Dest)
[structural]
syntax KItem ::= sprintf(KItem, K) [strict(1)]
context sprintf(_, HOLE:KItem => reval(HOLE)) [result(RValue)]
rule sprintf(formattedResult(S:String), tv(Dest::SymLoc, _))
=> writeString(Dest, S)
~> tv(lengthString(S), utype(int))
[structural]
rule builtin*("asprintf", Dest::RValue, Format::RValue, VarArgs:List)
=> asprintf(formatter(getString(Format), VarArgs), Dest)
[structural]
syntax KItem ::= asprintf(KItem, RValue) [strict(1)]
rule asprintf(formattedResult(S:String), Dest::RValue)
=> Computation((* Dest) := alignedAlloc(1, lengthString(S) +Int 1))
~> sprintf(formattedResult(S), (* Dest))
[structural]
rule <k> (.K => toHeatList(derefList(VarArgs)))
~> builtin("vsprintf", _, _, tv(opaque(VaList::Int, _), _))
...</k>
<va-lists>... VaList |-> VarArgs:List ...</va-lists>
[structural]
rule (.K => errorUninitVaList)
~> builtin("vsprintf", _, _, tv(V::CValue, _))
requires opaque(...) :/=K V
[structural]
rule <k> (.K => errorUninitVaList)
~> builtin("vsprintf", _, _, tv(opaque(VaList::Int, _), _))
...</k>
<va-lists> VaLists:Map </va-lists>
requires notBool VaList in_keys(VaLists)
[structural]
rule H:HeatList ~> builtin("vsprintf", Dest::RValue, Format::RValue, _)
=> sprintf(formatter(getString(Format), hListToList(H)), Dest)
requires isKResult(H)
[structural]
rule <k> (.K => toHeatList(derefList(VarArgs)))
~> builtin("vasprintf", _, _, tv(opaque(VaList::Int, _), _))
...</k>
<va-lists>... VaList |-> VarArgs:List ...</va-lists>
[structural]
rule (.K => errorUninitVaList)
~> builtin("vasprintf", _, _, tv(V::CValue, _))
requires opaque(...) :/=K V
[structural]
rule <k> (.K => errorUninitVaList)
~> builtin("vasprintf", _, _, tv(opaque(VaList::Int, _), _))
...</k>
<va-lists> VaLists:Map </va-lists>
requires notBool VaList in_keys(VaLists)
[structural]
rule H:HeatList ~> builtin("vasprintf", Dest::RValue, Format::RValue, _)
=> asprintf(formatter(getString(Format), hListToList(H)), Dest)
requires isKResult(H)
[structural]
syntax KItem ::= snprintf(KItem, SymLoc, Int) [strict(1)]
rule [snprintf]:
builtin*("snprintf", tv(Dest:SymLoc, _), tv(Len:Int, _), Format::RValue, VarArgs:List)
=> snprintf(formatter(getString(Format), VarArgs), Dest, Len)
[structural]
rule [snprintf-done-nz]:
snprintf(formattedResult(S:String), Dest:SymLoc, Len:Int)
=> writeString(Dest, substrString(S, 0, minInt(lengthString(S), Len -Int 1)))
~> tv(lengthString(S), utype(int))
requires Len >Int 0
[structural]
rule [snprintf-done-0]:
snprintf(formattedResult(S:String), _, 0)
=> tv(lengthString(S), utype(int))
[structural]
rule [vsnprintf]:
<k> (.K => toHeatList(derefList(VarArgs)))
~> builtin("vsnprintf", _, _, _, tv(opaque(VaList::Int, _), _))
...</k>
<va-lists>... VaList |-> VarArgs:List ...</va-lists>
[structural]
rule (.K => errorUninitVaList)
~> builtin("vsnprintf", _, _, _, tv(V::CValue, _))
requires opaque(...) :/=K V
[structural]
rule <k> (.K => errorUninitVaList)
~> builtin("vsnprintf", _, _, _, tv(opaque(VaList::Int, _), _))
...</k>
<va-lists> VaLists:Map </va-lists>
requires notBool VaList in_keys(VaLists)
[structural]
rule H:HeatList ~> builtin("vsnprintf", tv(Dest::SymLoc, _), tv(Len::Int, _), Format::RValue, _)
=> snprintf(formatter(getString(Format), hListToList(H)), Dest, Len)
requires isKResult(H)
[structural]
rule builtin*("__isoc99_scanf" => "scanf", _:RValue, _)
[structural]
rule builtin*("scanf", Format::RValue, VarArgs:List)
=> fscanf(stdin, getString(Format), VarArgs)
[structural]
rule builtin*("__isoc99_fscanf" => "fscanf", _, _, _)
[structural]
rule builtin*("fscanf", FDPtr::RValue, Format::RValue, VarArgs:List)
=> fscanf((* FDPtr), getString(Format), VarArgs)
[structural]
rule builtin("__isoc99_vfscanf" => "vfscanf", _, _, _)
[structural]
rule <k> (.K => toHeatList(derefList(VarArgs)))
~> builtin("vfscanf", _, _, tv(VaList::Int, _))
...</k>
<va-lists>... VaList |-> VarArgs:List ...</va-lists>
[structural]
rule H:HeatList ~> builtin("vfscanf", FDPtr::RValue, Format::RValue, _)
=> fscanf((* FDPtr), getString(Format), hListToList(H))
requires isKResult(H)
[structural]
rule builtin("__isoc99_vscanf" => "vscanf", _, _)
[structural]
rule <k> (.K => toHeatList(derefList(VarArgs)))
~> builtin("vscanf", _, tv(VaList::Int, _))
...</k>
<va-lists>... VaList |-> VarArgs:List ...</va-lists>
[structural]
rule H:HeatList ~> builtin("vscanf", Format::RValue, _)
=> fscanf(stdin, getString(Format), hListToList(H))
requires isKResult(H)
[structural]
rule builtin*("__isoc99_sscanf" => "sscanf", _, _, _)
[structural]
rule builtin*("sscanf", Str::RValue, Format::RValue, VarArgs:List)
=> fscanf(getString(Str), getString(Format), VarArgs)
[structural]
rule builtin("__isoc99_vsscanf" => "vsscanf", _, _, _)
[structural]
rule <k> (.K => toHeatList(derefList(VarArgs)))
~> builtin("vsscanf", _, _, tv(VaList::Int, _))
...</k>
<va-lists>... VaList |-> VarArgs:List ...</va-lists>
[structural]
rule H:HeatList ~> builtin("vsscanf", Str::RValue, Format::RValue, _)
=> fscanf(getString(Str), getString(Format), hListToList(H))
requires isKResult(H)
[structural]
syntax KItem ::= fscanf(K, KItem, List) [strict(2)]
context fscanf(HOLE:KItem => reval(HOLE), _, _) [result(RValue)]
rule <k> fscanf(tv(opaque(FD:Int, _), _), str(S::String), VarArgs::List)
=> fscanf'(FD, VarArgs, parseFormat(stringToList(S)), 0, "", 0)
...</k>
<open-files> OpenFiles:Map </open-files>
requires FD in_keys(OpenFiles)
[structural]
rule <k> fscanf(tv(_, _) #as V:RValue, _, _) => eof ...</k>
<open-files> OpenFiles:Map </open-files>
requires (getFD(V) ==K "")
orBool notBool (getFD(V) in_keys(OpenFiles))
[structural]
rule <k> fscanf(str(FD::String), str(S::String), VarArgs::List)
=> fscanf'(FD, VarArgs, parseFormat(stringToList(S)), 0, "", 0)
...</k>
<open-files>... .Map => FD |-> printStackTrace(L, Tu, Sc, Loc, Env) </open-files>
<call-stack> L:List </call-stack>
<curr-tu> Tu::String </curr-tu>
<renv> Env::Map </renv>
<curr-program-loc> Loc:CabsLoc </curr-program-loc>
<curr-scope> Sc:Scope </curr-scope>
<files>...
(.Bag =>
<file>...
<fid> FD </fid>
<read-buffer> FD </read-buffer>
...</file>
)
...</files>
[structural]
// FD, args, parsed format string, number matched, current item, number of chars read
syntax KItem ::= "fscanf'" "(" FauxFD "," List "," List "," Int "," String "," Int")"
rule <k> (.K => readFD(FD, 1))
~> fscanf'(FD::FauxFD, _, ListItem(D::Directive) _, _, Item::String, _)
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
<read-buffer> Buffer::String </read-buffer>
<feof> Eof:Bool </feof>
<ferror> 0 </ferror>
requires isReadable(Mode)
andBool notBool atFieldWidth(Item, D)
andBool notBool (Buffer ==K "" andBool Eof)
[structural]
rule <k> fscanf'(FD::FD, _, ListItem(D::Directive) _, Matched::Int, Item::String, _)
=> #if Matched ==Int 0 #then eof #else tv(Matched, utype(int)) #fi
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
<read-buffer> Buffer::String </read-buffer>
<feof> Eof:Bool </feof>
<ferror> Err:Int </ferror>
requires notBool ((Buffer ==K "" andBool Eof) orBool atFieldWidth(Item, D))
andBool Err =/=Int 0
andBool isReadable(Mode)
[structural]
rule <k> fscanf'(FD::FD, _, _, Matched::Int, _, _)
=> #if Matched ==Int 0 #then eof #else tv(Matched, utype(int)) #fi
...</k>
<fid> FD </fid>
<mode> Mode:String </mode>
<ferror> _ => getErrno(#EBADF) </ferror>
requires notBool isReadable(Mode)
[structural]
rule <k> fscanf'(FD::FD, _,
(ListItem(whitespace) => .List) _,
_,
_ => "",
_)
...</k>
<fid> FD </fid>
<read-buffer> "" </read-buffer>
<feof> true </feof>
[structural]
rule <k> (.K => writeItem(D, Item, Arg, NRead))
~> fscanf'(FD::FD, (ListItem(Arg::RValue) => .List) _,
(ListItem(%(Wr::WriteBehavior, _, _, _) #as D::Directive) => .List) _,
M::Int => #if isReadCountDirective(D) #then M #else M +Int 1 #fi,
Item::String => "",
NRead::Int)
...</k>
<fid> FD </fid>
<read-buffer> Buffer::String </read-buffer>
<feof> Eof:Bool </feof>
requires ((Buffer ==K "" andBool Eof) orBool atFieldWidth(Item, D)) andBool isCompleteItem(Item, D)
andBool (Wr ==K dirWrite orBool Wr ==K dirAlloc)
[structural]
rule <k> fscanf'(FD::FD, _,
(ListItem(%(dirRead, _, _, _) #as D::Directive) => .List) _,
M::Int => M +Int 1,
Item::String => "",
_)
...</k>
<fid> FD </fid>
<read-buffer> Buffer::String </read-buffer>
<feof> Eof:Bool </feof>
requires ((Buffer ==K "" andBool Eof) orBool atFieldWidth(Item, D)) andBool isCompleteItem(Item, D)
[structural]
rule <k> fscanf'(FD::FD, _,
ListItem(D::Directive) _,
Matched::Int,
Item::String,
_)