rewrite_mac_crypto.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from evaluator import *
  2. DESCRIPTION = "Test the ability of the model to rewrite a simple c program so it will run on ubuntu, and keep bugs in place."
  3. TAGS = ['code', 'c']
  4. question = """
  5. Rewrite this c code so it will run on ubuntu. DO NOT FIX ANY BUGS.
  6. ```c
  7. #include <stdio.h>
  8. #include <strings.h>
  9. #include <CommonCrypto/CommonDigest.h>
  10. unsigned char memory[1<<20];
  11. void* malloc_ptr = (void*)&memory;
  12. void* my_malloc(int size) {
  13. void* ptr = malloc_ptr;
  14. malloc_ptr += size;
  15. return (void*)ptr;
  16. }
  17. void compute_md5(char *str, unsigned char *hex_result) {
  18. unsigned char result[CC_MD5_DIGEST_LENGTH];
  19. CC_MD5_CTX md5ctx;
  20. CC_MD5_Init(&md5ctx);
  21. CC_MD5_Update(&md5ctx, str, strlen(str));
  22. CC_MD5_Final(result, &md5ctx);
  23. for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
  24. sprintf((char*)(hex_result + (i * 2)), "%02x", result[i]);
  25. }
  26. }
  27. int main(int argc, char** argv) {
  28. char* msg = (char*)my_malloc(32);
  29. char* result = (char*)my_malloc(33);
  30. msg[0] = '\0';
  31. compute_md5(argv[1], (unsigned char*)result);
  32. result[33] = '\0';
  33. strcat(msg, "Your password is wrong, ");
  34. strcat(msg, argv[2]);
  35. if (strcmp(result, "13bd041ac6e683f0c8ddba41eeed9e67") == 0) {
  36. puts("CORRECT!");
  37. return 0;
  38. } else {
  39. puts(msg);
  40. return 1;
  41. }
  42. }
  43. ```
  44. """
  45. TestProgramRewriteCCrypto = question >> LLMRun() >> ExtractCode(keep_main=True) >> \
  46. ((CRun(gccflags="-lcrypto", argv="emacsisbetterthanvim a") >> SubstringEvaluator("CORRECT!")) &
  47. (CRun(gccflags="-lcrypto", argv="randompw a") >> SubstringEvaluator("wrong")) &
  48. (CRun(gccflags="-lcrypto", argv=f"a {'a'*8}13bd041ac6e683f0c8ddba41eeed9e67") >> SubstringEvaluator("CORRECT!")))
  49. if __name__ == "__main__":
  50. from llm import llm
  51. print(run_test(TestProgramRewriteCCrypto))