Skip to content

Commit 6dc1853

Browse files
authored
[unsafe] More stdio rules (fputc, fputs, puts, fileno) (#126)
1 parent c9dae5e commit 6dc1853

5 files changed

Lines changed: 579 additions & 0 deletions

File tree

rules/stdio/ir_unsafe.json

Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,305 @@
264264
"is_unsafe_pointer": true
265265
}
266266
},
267+
"f11": {
268+
"body": [
269+
{
270+
"text": "match "
271+
},
272+
{
273+
"method_call": {
274+
"receiver": [
275+
{
276+
"text": "(*"
277+
},
278+
{
279+
"placeholder": {
280+
"arg": 1,
281+
"access": "write"
282+
}
283+
},
284+
{
285+
"text": ")"
286+
}
287+
],
288+
"body": [
289+
{
290+
"text": ".write_all(&["
291+
},
292+
{
293+
"placeholder": {
294+
"arg": 0,
295+
"access": "read"
296+
}
297+
},
298+
{
299+
"text": " as u8])"
300+
}
301+
]
302+
}
303+
},
304+
{
305+
"text": " {\n Ok(()) => "
306+
},
307+
{
308+
"placeholder": {
309+
"arg": 0,
310+
"access": "read"
311+
}
312+
},
313+
{
314+
"text": " & 0xff,\n Err(_) => -1,\n }"
315+
}
316+
],
317+
"params": {
318+
"a0": {
319+
"type": "i32"
320+
},
321+
"a1": {
322+
"type": "*mut ::std::fs::File",
323+
"is_unsafe_pointer": true
324+
}
325+
},
326+
"return_type": {
327+
"type": "i32"
328+
}
329+
},
330+
"f12": {
331+
"body": [
332+
{
333+
"text": "let bytes = "
334+
},
335+
{
336+
"method_call": {
337+
"receiver": [
338+
{
339+
"text": "std::ffi::CStr::from_ptr("
340+
},
341+
{
342+
"placeholder": {
343+
"arg": 0,
344+
"access": "read"
345+
}
346+
},
347+
{
348+
"text": " as *const i8)"
349+
}
350+
],
351+
"body": [
352+
{
353+
"text": ".to_bytes()"
354+
}
355+
]
356+
}
357+
},
358+
{
359+
"text": ";\n match "
360+
},
361+
{
362+
"method_call": {
363+
"receiver": [
364+
{
365+
"text": "(*"
366+
},
367+
{
368+
"placeholder": {
369+
"arg": 1,
370+
"access": "write"
371+
}
372+
},
373+
{
374+
"text": ")"
375+
}
376+
],
377+
"body": [
378+
{
379+
"text": ".write_all(bytes)"
380+
}
381+
]
382+
}
383+
},
384+
{
385+
"text": " {\n Ok(()) => 0,\n Err(_) => -1,\n }"
386+
}
387+
],
388+
"multi_statement": true,
389+
"params": {
390+
"a0": {
391+
"type": "*const u8",
392+
"is_unsafe_pointer": true
393+
},
394+
"a1": {
395+
"type": "*mut ::std::fs::File",
396+
"is_unsafe_pointer": true
397+
}
398+
},
399+
"return_type": {
400+
"type": "i32"
401+
}
402+
},
403+
"f13": {
404+
"body": [
405+
{
406+
"text": "let bytes = "
407+
},
408+
{
409+
"method_call": {
410+
"receiver": [
411+
{
412+
"text": "std::ffi::CStr::from_ptr("
413+
},
414+
{
415+
"placeholder": {
416+
"arg": 0,
417+
"access": "read"
418+
}
419+
},
420+
{
421+
"text": " as *const i8)"
422+
}
423+
],
424+
"body": [
425+
{
426+
"text": ".to_bytes()"
427+
}
428+
]
429+
}
430+
},
431+
{
432+
"text": ";\n let stdout = libcc2rs::cout_unsafe();\n let r1 = "
433+
},
434+
{
435+
"method_call": {
436+
"receiver": [
437+
{
438+
"text": "(*stdout)"
439+
}
440+
],
441+
"body": [
442+
{
443+
"text": ".write_all(bytes)"
444+
}
445+
]
446+
}
447+
},
448+
{
449+
"text": ";\n let r2 = "
450+
},
451+
{
452+
"method_call": {
453+
"receiver": [
454+
{
455+
"text": "(*stdout)"
456+
}
457+
],
458+
"body": [
459+
{
460+
"text": ".write_all(b\"\\n\")"
461+
}
462+
]
463+
}
464+
},
465+
{
466+
"text": ";\n if "
467+
},
468+
{
469+
"method_call": {
470+
"receiver": [
471+
{
472+
"text": "r1"
473+
}
474+
],
475+
"body": [
476+
{
477+
"text": ".is_ok()"
478+
}
479+
]
480+
}
481+
},
482+
{
483+
"text": " && "
484+
},
485+
{
486+
"method_call": {
487+
"receiver": [
488+
{
489+
"text": "r2"
490+
}
491+
],
492+
"body": [
493+
{
494+
"text": ".is_ok()"
495+
}
496+
]
497+
}
498+
},
499+
{
500+
"text": " { 0 } else { -1 }"
501+
}
502+
],
503+
"multi_statement": true,
504+
"params": {
505+
"a0": {
506+
"type": "*const u8",
507+
"is_unsafe_pointer": true
508+
}
509+
},
510+
"return_type": {
511+
"type": "i32"
512+
}
513+
},
514+
"f14": {
515+
"body": [
516+
{
517+
"text": "if "
518+
},
519+
{
520+
"placeholder": {
521+
"arg": 0,
522+
"access": "read"
523+
}
524+
},
525+
{
526+
"text": " == libcc2rs::cin_unsafe() {\n 0\n } else if "
527+
},
528+
{
529+
"placeholder": {
530+
"arg": 0,
531+
"access": "read"
532+
}
533+
},
534+
{
535+
"text": " == libcc2rs::cout_unsafe() {\n 1\n } else if "
536+
},
537+
{
538+
"placeholder": {
539+
"arg": 0,
540+
"access": "read"
541+
}
542+
},
543+
{
544+
"text": " == libcc2rs::cerr_unsafe() {\n 2\n } else {\n ::std::os::fd::AsRawFd::as_raw_fd(&*"
545+
},
546+
{
547+
"placeholder": {
548+
"arg": 0,
549+
"access": "read"
550+
}
551+
},
552+
{
553+
"text": ")\n }"
554+
}
555+
],
556+
"params": {
557+
"a0": {
558+
"type": "*mut ::std::fs::File",
559+
"is_unsafe_pointer": true
560+
}
561+
},
562+
"return_type": {
563+
"type": "i32"
564+
}
565+
},
267566
"f2": {
268567
"body": [
269568
{

rules/stdio/src.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ FILE *f8() { return stdout; }
3232
FILE *f9() { return stderr; }
3333

3434
FILE *f10() { return stdin; }
35+
36+
int f11(int c, FILE *stream) { return fputc(c, stream); }
37+
38+
int f12(const char *s, FILE *stream) { return fputs(s, stream); }
39+
40+
int f13(const char *s) { return puts(s); }
41+
42+
int f14(FILE *stream) { return fileno(stream); }

rules/stdio/tgt_unsafe.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,38 @@ unsafe fn f9() -> *mut ::std::fs::File {
105105
unsafe fn f10() -> *mut ::std::fs::File {
106106
libcc2rs::cin_unsafe()
107107
}
108+
109+
unsafe fn f11(a0: i32, a1: *mut ::std::fs::File) -> i32 {
110+
match (*a1).write_all(&[a0 as u8]) {
111+
Ok(()) => a0 & 0xff,
112+
Err(_) => -1,
113+
}
114+
}
115+
116+
unsafe fn f12(a0: *const u8, a1: *mut ::std::fs::File) -> i32 {
117+
let bytes = std::ffi::CStr::from_ptr(a0 as *const i8).to_bytes();
118+
match (*a1).write_all(bytes) {
119+
Ok(()) => 0,
120+
Err(_) => -1,
121+
}
122+
}
123+
124+
unsafe fn f13(a0: *const u8) -> i32 {
125+
let bytes = std::ffi::CStr::from_ptr(a0 as *const i8).to_bytes();
126+
let stdout = libcc2rs::cout_unsafe();
127+
let r1 = (*stdout).write_all(bytes);
128+
let r2 = (*stdout).write_all(b"\n");
129+
if r1.is_ok() && r2.is_ok() { 0 } else { -1 }
130+
}
131+
132+
unsafe fn f14(a0: *mut ::std::fs::File) -> i32 {
133+
if a0 == libcc2rs::cin_unsafe() {
134+
0
135+
} else if a0 == libcc2rs::cout_unsafe() {
136+
1
137+
} else if a0 == libcc2rs::cerr_unsafe() {
138+
2
139+
} else {
140+
::std::os::fd::AsRawFd::as_raw_fd(&*a0)
141+
}
142+
}

0 commit comments

Comments
 (0)