int main(void) { __builtin_printf ("%s %s\n", __DATE__, __TIME__); /* { dg-error "SOURCE_DATE_EPOCH must expand" } */ __builtin_printf ("%s %s\n", __DATE__, __TIME__); return 0; }
void foo () { int l, h; for (l = 0; l <= 7; l++) { int j = 38; if (g) h = 0; for (; h <= 7; h++) { int i, k = b % (j % 4); g = f; for (;;) { j = 6 || b; if (e) { for (; j; --j) if (k) __builtin_printf ("%d", 9); if (i) __builtin_printf ("%d", j); } if (l) continue; break; } i = f || b; } } }
void foo (int i) { __builtin_fprintf (stdout, "%d", i); __builtin_fprintf (stdout, "%ld", i); /* { dg-warning "format" "__builtin_fprintf" } */ __builtin_printf ("%d", i); __builtin_printf ("%ld", i); /* { dg-warning "format" "__builtin_printf" } */ __builtin_fprintf_unlocked (stdout, "%d", i); __builtin_fprintf_unlocked (stdout, "%ld", i); /* { dg-warning "format" "__builtin_fprintf_unlocked" } */ __builtin_printf_unlocked ("%d", i); __builtin_printf_unlocked ("%ld", i); /* { dg-warning "format" "__builtin_printf_unlocked" } */ }
int main () { char s[10]; const int t = (__builtin_memcpy (s, "Hello", 6), 777); __builtin_printf ("%d %s\n", t, s); return 0; }
int main (void) { test_c ('?'); test_d_i (0xdeadbeef, 0xdeadbeefL); test_x ('?', 0xdead, 0xdeadbeef); test_a_double (0.0); test_e_double (0.0); test_f_double (0.0); test_g_double (0.0); test_a_long_double (); test_e_long_double (0.0); test_f_long_double (); test_g_long_double (); test_s (0); test_n (); test_percent (); if (nfails) { __builtin_printf ("%u out of %u tests failed\n", nfails, ntests); __builtin_abort (); } return 0; }
runtime_assert (int expr, int line) { if (!expr) { __builtin_printf ("line %i: assertion failed\n", line); ++nfails; } }
int bar (int i) { try { __builtin_printf ("foo %d\n", i); } catch (...) { callme(); } }
int main (int argc, char **argv) { int a[N]; for (int i = 0; i < N; i++) a[i] = 2*i + 1; int *p = &a[0]; __builtin_printf ("%d\n", a[argc]); return *(++p); }
int main (int argc, char **argv) { struct S0 d[] = { { 1 }, { 2 } }; struct S0 e = d[1]; c = d[0].f0; __builtin_printf ("%x\n", e.f0); return 0; }
void test_const_arrays (void) { /* TODO: ideally we'd highlight both the format string *and* the use of it here. For now, just verify that we gracefully handle this case. */ const char a[] = " %d "; __builtin_printf(a, 0.5); /* { dg-warning "20: format .%d. expects argument of type .int., but argument 2 has type .double." } */ /* { dg-begin-multiline-output "" } __builtin_printf(a, 0.5); ^ { dg-end-multiline-output "" } */ }
void load_segment(const char *filename, uint8_t *position) { __builtin_printf("--- Mapping Memory ---------------------\n"); struct stat stat_d; auto handle = open(filename, O_RDONLY); if(handle == -1) { __builtin_printf("Failed: open(%s, O_RDONLY)", filename); exit(1); } fstat(handle, &stat_d); __builtin_printf("Opening: %s (ID: %d, Size: %d)\n", filename, handle, stat_d.st_size); auto memory = (uint8_t*)mmap ( nullptr , stat_d.st_size , PROT_READ , MAP_SHARED , handle , 0 ); __builtin_printf("Mapping: 0x%x\n", memory); memcpy ( position , memory , stat_d.st_size ); __builtin_printf("Copying: %s\n", (memory[0] == position[0]) ? "done" : "failed"); __builtin_printf("----------------------------------------\n\n"); }
int main (int argc, char **argv) { union { int a[4]; int b[2]; } u; u.a[0] = 1; u.a[1] = 42; u.a[2] = 3; u.a[3] = 4; __builtin_printf ("%d\n", u.a[argc]); return u.b[1]; }
void test_non_contiguous_strings (void) { __builtin_printf(" %" "d ", 0.5); /* { dg-warning "20: format .%d. expects argument of type .int., but argument 2 has type .double." } */ /* { dg-message "26: format string is defined here" "" { target *-*-* } .-1 } */ /* { dg-begin-multiline-output "" } __builtin_printf(" %" "d ", 0.5); ^~~~ { dg-end-multiline-output "" } */ /* { dg-begin-multiline-output "" } __builtin_printf(" %" "d ", 0.5); ~~~~^ %" "f { dg-end-multiline-output "" } */ }
checkv (const char *func, int line, int res, int min, int max, char *dst, const char *fmt, __builtin_va_list va) { int n = __builtin_vsprintf (dst, fmt, va); int len = __builtin_strlen (dst); ++ntests; int fail = 0; if (n != res) { __builtin_printf ("FAIL: %s:%i: \"%s\" expected result for \"%s\" " "doesn't match function call return value: ", func, line, fmt, dst); if (min == max) __builtin_printf ("%i != %i\n", n, min); else __builtin_printf ("%i not in [%i, %i]\n", n, min, max); fail = 1; } else { if (len < min || max < len) { __builtin_printf ("FAIL: %s:%i: \"%s\" expected result for \"%s\" " "doesn't match output length: ", func, line, fmt, dst); if (min == max) __builtin_printf ("%i != %i\n", len, min); else __builtin_printf ("%i not in [%i, %i]\n", len, min, max); fail = 1; } else if (min == max) __builtin_printf ("PASS: %s:%i: \"%s\" result %i: \"%s\"\n", func, line, fmt, n, dst); else __builtin_printf ("PASS: %s:%i: \"%s\" result %i in [%i, %i]: \"%s\"\n", func, line, fmt, n, min, max, dst); } if (fail) ++nfails; }
void foo (int x) { unsigned int a = 0; int b = 3; if (x) b = 0; lab: if (x) goto lab; a++; if (b != 2) __builtin_printf ("%u", a); goto lab; }
void foo (void) { __builtin_printf ("%u\n", uc); /* { dg-bogus "expects argument of type" } */ __builtin_printf ("%u\n", sc); /* { dg-warning "expects argument of type" } */ __builtin_printf ("%u\n", us); /* { dg-bogus "expects argument of type" } */ __builtin_printf ("%u\n", ss); /* { dg-warning "expects argument of type" } */ __builtin_printf ("%u\n", u); /* { dg-bogus "expects argument of type" } */ __builtin_printf ("%u\n", i); /* { dg-warning "expects argument of type" } */ }
void fn1() { int i; for (;;) { i = 0; for (; i < 6; i++) { if (b) for (;;) ; int c = a[i]; __builtin_printf("", i); } } }
int main () { int i; char c; char arr[11]; for (i = 0; i < 10; i++) { c = 0x30 + i; foo (&arr[i], c); /* { dg-final { gdb-test 22 "i" "c - 48" } } */ } arr[10] = 0; __builtin_printf ("arr = %s\n", arr); return 0; }
foo (double a, double q, double *ff, double *gx, int e, int ni) { union { double n; unsigned long long o; } punner; double d; punner.n = q; __builtin_printf("B: 0x%016llx ---- %g\n", punner.o, q); d = q - 5; if(d < 0) d = -d; if (d > 0.1) __builtin_abort(); }
int rd (int *pppp, int n, ...) { va_list argp; int *p; int i; int res; va_start (argp, n); for (; n > 0; n--) va_arg (argp, double); p = va_arg (argp, int *); i = va_arg (argp, int); res = p[i]; __builtin_printf ("%d\n", res); return res; }
int main () { for (; a; a--) { int i; if (h && i) __builtin_printf ("%d%d", c, f); i = 0; for (; i < 2; i++) if (g) for (; d < 10; d++) b = *e; i = 0; for (; i < 1; i++) ; } exit (0); }
void main_test (void) { const char *const s1 = "hello world"; const char *const s2[] = { s1, 0 }, *const*s3; printf ("%s\n", "hello"); printf ("%s\n", *s2); s3 = s2; printf ("%s\n", *s3++); if (s3 != s2+1 || *s3 != 0) abort(); printf ("%c", '\n'); printf ("%c", **s2); s3 = s2; printf ("%c", **s3++); if (s3 != s2+1 || *s3 != 0) abort(); printf (""); printf ("%s", ""); printf ("\n"); printf ("%s", "\n"); printf ("hello world\n"); printf ("%s", "hello world\n"); /* Test at least one instance of the __builtin_ style. We do this to ensure that it works and that the prototype is correct. */ __builtin_printf ("%s\n", "hello"); /* These builtin stubs are called by __builtin_printf, ensure their prototypes are set correctly too. */ __builtin_putchar ('\n'); __builtin_puts ("hello"); /* Check the unlocked style, these evaluate to nothing to avoid problems on systems without the unlocked functions. */ printf_unlocked (""); __builtin_printf_unlocked (""); printf_unlocked ("%s", ""); __builtin_printf_unlocked ("%s", ""); }
int main (unsigned char argc, char **argv) { unsigned char in[16]; unsigned char i = 0; unsigned char sum = 1; for (i = 0; i < 16; i++) in[i] = i + i + 1; for (i = 0; i < 16; i++) sum *= in[i]; if (sum != 33) { __builtin_printf("Failed %d\n", sum); abort(); } return 0; }
int main (unsigned char argc, char **argv) { unsigned char i = 0; unsigned char sum = 1; for (i = 0; i < 16; i++) in[i] = (i + i + 1) & 0xfd; /* Prevent constant propagation of the entire loop below. */ asm volatile ("" : : : "memory"); for (i = 0; i < 16; i++) sum |= in[i]; if (sum != 29) { __builtin_printf("Failed %d\n", sum); abort(); } return 0; }
int main() { /* * ------------------------------------------------------------------------ * Allocate VM Memory. * * First 4096 Bytes: VM Itself * Next 16384 Bytes: Instruction Cache and Shadow Instruction Cache * Final 8192 Bytes: VM Stack. * ------------------------------------------------------------------------ */ auto buffer = (uint8_t*)mmap ( nullptr , 28672 , PROT_EXEC | PROT_READ | PROT_WRITE , MAP_PRIVATE | MAP_ANONYMOUS , -1 , 0 ); /* Initialize Virtual Machine. */ load_segment("vm_a.bin", buffer); load_segment("cache.bin", buffer + 4096); /* Load, Create, and Scramble Instruction Index. */ struct Instruction { size_t index; const char *name; bool operator==(Instruction const& other) const { return strncmp(name, other.name, strlen(name)) == 0; } }; Instruction instruction_index[] = { #include "instruction_cache.hpp" }; __builtin_printf("--- Shuffling Instructions--------------\n"); Instruction shuffling_index[] = { #include "instruction_cache.hpp" }; // Copy the Index to keep he original intact. std::copy(std::begin(instruction_index), std::end(instruction_index), std::begin(shuffling_index)); // Randomly Shuffle the Index srand(time(0)); std::random_shuffle ( std::begin(shuffling_index) , std::end(shuffling_index) - 1 ); // Keeps Track of Write Position in Shadow Instruction Cache. size_t shuffle_offset = 0; // Iterate Shuffled Index and Create that in the Shadow Cache. for(auto &entry : shuffling_index) { __builtin_printf("Index(%02d): %s\n", ((size_t)&entry - (size_t)shuffling_index) / sizeof(Instruction), entry.name); // Find instruction in the old index, in order to find its size. auto instruction = std::find ( std::begin(instruction_index) , std::end(instruction_index) - 1 , entry ); // Find size of instruction. signed difference = (instruction + 1)->index - instruction->index + 1; // Ignore the last dummy instruction (always has a negative offset) if(difference > 0) { __builtin_printf ( "Distance: (%d - %d) = %d\n" , (instruction + 1)->index , instruction->index , difference ); // Write the instruction into Shadow Cache. memcpy ( buffer + 8192 , buffer + 4096 + entry.index , difference ); // Update the Index. entry.index = shuffle_offset; shuffle_offset += difference; } } __builtin_printf("----------------------------------------\n\n"); /* * ------------------------------------------------------------------------ * Display instruction confirmations. * ------------------------------------------------------------------------ */ for(auto &entry : instruction_index) { auto shuffled = std::find ( std::begin(shuffling_index) , std::end(shuffling_index) , entry ); __builtin_printf ( "%12s: 0x%x 0x%x\n" , entry.name , *(buffer + 4096 + entry.index) , *(buffer + 8192 + shuffled->index) ); } /* * ------------------------------------------------------------------------ * Compiling VM assembly. * ------------------------------------------------------------------------ */ size_t program[] = { 2, (size_t)buffer + 4096, (size_t)buffer + 4096, (size_t)buffer + 4096, 0, (size_t)buffer + 4096, (size_t)buffer + 4096, (size_t)buffer + 4096, 0 }; int result = Metamorph(buffer,program); __builtin_printf("Result: %d\n\n", result); }
__attribute__((__noinline__, __noclone__, __stdcall__)) void g(int a) { __builtin_printf("in g(): %d\n", a); }
int main() { __builtin_printf("%d\n", o.obj()->val()); }
__attribute__((__noinline__, __noclone__, __thiscall__)) void h(int a, int b) { __builtin_printf("in h(): %d %d\n", a, b); }
void fn1 (void) { __builtin_printf ("xxxxxxxxxxxxxxxxx%dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); /* { dg-warning "format" } */ }
// <rdar://problem/8336581> void test15(const char *s) { __builtin_printf("string is %s\n", s); }