编译 | 郑丽媛
来源 | CSDN(ID:CSDNnews)
全文共 4238 个字,建议阅读 10 分钟
/* Copyright 2023. All unauthorized distribution of this source code will be persecuted to the fullest extent of the law*/#include <stdio.h>#include <stdint.h>#include <stdlib.h>int main(int argc, char* argv[]){ uint8_t number = atoi(argv[1]); // No problems here if (number == 0) printf("evenn"); if (number == 1) printf("oddn"); if (number == 2) printf("evenn"); if (number == 3) printf("oddn"); if (number == 4) printf("evenn"); if (number == 5) printf("oddn"); if (number == 6) printf("evenn"); if (number == 7) printf("oddn"); if (number == 8) printf("evenn"); if (number == 9) printf("oddn"); if (number == 10) printf("evenn");}
PS > cl.exe /Od program.c
PS > .program.exe 0
even
PS > .program.exe 4
even
PS > .program.exe 3
odd
PS > .program.exe 7
odd
PS > .program.exe 50
PS > .program.exe 11
PS > .program.exe 99
print("/* Copyright 2023. All unauthorized distribution of this source code")
print(" will be persecuted to the fullest extent of the law*/")
print("#include <stdio.h>")
print("#include <stdint.h>")
print("#include <stdlib.h>")
print("int main(int argc, char* argv[])")
print("{")
print(" uint8_t number = atoi(argv[1]); // No problems here")
for i in range(2**8):
print(" if (number == "+str(i)+")")
if i % 2 == 0:
print(" printf("even\n");")
else:
print(" printf("odd\n");")
print("}")
PS > python programmer.py > program.c
PS > cl.exe /Od program.c
PS > .program.exe 99
odd
PS > .program.exe 50
even
PS > .program.exe 240
even
PS > .program.exe 241
odd
print(" uint16_t number = atoi(argv[1]); // No problems here")
…
for i in range(2**16):
PS > python programmer.py > program.c
PS > cl.exe /Od program.c
PS > .program.exe 21000
even
PS > .program.exe 3475
odd
PS > .program.exe 3
odd
PS > .program.exe 65001
odd
PS > .program.exe 65532
even
print(" uint32_t number = atoi(argv[1]); // No problems here")
…
for i in range(2**32):
PS > cl /Od program.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.32.31329 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
program.c
program.c(134397076): warning C4049: compiler limit: terminating line number emission
program.c(134397076): note: Compiler limit for line number is 16777215
program.c(41133672): fatal error C1060: compiler is out of heap space
; Argument is stored in ECX, return value in EAX
XOR EAX, EAX ; Set eax to zero (return value for odd number)
CMP ECX, 0h ; Compare arg to 0
JNE 3h ; Skip next two instructions if it wasn't equal
INC EAX ; It was even, set even return value (1)
RET ; Return
CMP ECX, 1h ; Compare arg to 1
JNE 2 ; Skip next instruction if not equal
RET ; Odd return value already in EAX, just RET
; add the next 2...2^32-1 comparisons here
RET ; Fallback return
import struct
with open('isEven.bin', 'wb') as file:
file.write(b"x31xC0") # XOR EAX, EAX
for i in range(2**32):
ib = struct.pack("<I", i) # Encode i as 32 bit little endian integer
file.write(b"x81xF9" + ib) # CMP ECX, i
if i%2 == 0:
file.write(b"x75x03") # JNE +3
file.write(b"xFFxC0") # INC EAX
file.write(b"xC3") # RET
else:
file.write(b"x75x01") # JNE +1
file.write(b"xC3") # RET
file.write(b"xC3") # Fallback RET
#include <stdio.h>
#include <Windows.h>
#include <stdint.h>
int main(int argc, char* argv[])
{
uint32_t number = atoi(argv[1]); // No problems here
// Open code file
HANDLE binFile = CreateFileA(
"isEven.bin",
GENERIC_READ | GENERIC_EXECUTE, FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
// Get 64 bit size of file
LARGE_INTEGER codeSize;
GetFileSizeEx(binFile, &codeSize);
// Create memory map of the file
HANDLE mapping = CreateFileMapping(
binFile,
NULL,
PAGE_EXECUTE_READ,
0,
0,
NULL);
// Get a pointer to the code
LPVOID code = MapViewOfFile(
mapping,FILE_MAP_EXECUTE | FILE_MAP_READ,
0,
0,
codeSize.QuadPart);
// Create a function that points to the code
int (*isEven)(int) = (int(*)(int))code;
if (isEven(number))
printf("evenn");
else
printf("oddn");
CloseHandle(binFile);
}
PS >.program.exe 300
even
PS >.program.exe 0
even
PS >.program.exe 1000000
even
PS >.program.exe 100000007
odd
PS >.program.exe 400000000
even
PS >.program.exe 400000001
odd
PS >.program.exe 400000006
even
PS >.program.exe 4200000000
odd <---- WRONG!
uint32_t number = strtoul(argv[1], NULL, 10);// No problems here
PS >.program.exe 4200000000
even
PS >.program.exe 4200000001
odd
-
“在我看来,这几乎是过度设计。为什么要费尽心思生成代码?只需一个简单的‘for 循环’就能解决。”
func isOdd(n int) bool {
var odd bool
for i := 0; i < n; i++ {
odd = !odd
}
return odd
}
-
“真正高质量的运行应始终使用递归。”
func isOdd(n int) bool {
switch {
case n == 0:
return false
case n > 0:
return !isOdd(n-1)
default:
return !isOdd(n+1)
}
}
参考链接:
https://andreasjhkarlsson.github.io/jekyll/update/2023/12/27/4-billion-if-statements.html
【End】
据统计,99%的数据大咖都关注了这个公众号
👇
原文始发于微信公众号(谈数据):用 40 亿条 if 语句,只为判断一个数字是奇是偶?
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论