Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: runtime/vm/precompiler.h

Issue 3003583002: [VM, Precompiler] PoC Obfuscator (Closed)
Patch Set: address comments and fix stuff Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #ifndef RUNTIME_VM_PRECOMPILER_H_ 5 #ifndef RUNTIME_VM_PRECOMPILER_H_
6 #define RUNTIME_VM_PRECOMPILER_H_ 6 #define RUNTIME_VM_PRECOMPILER_H_
7 7
8 #include "vm/allocation.h" 8 #include "vm/allocation.h"
9 #include "vm/hash_map.h" 9 #include "vm/hash_map.h"
10 #include "vm/hash_table.h" 10 #include "vm/hash_table.h"
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 void DropTypeArguments(); 391 void DropTypeArguments();
392 void DropScriptData(); 392 void DropScriptData();
393 void DropLibraryEntries(); 393 void DropLibraryEntries();
394 void DropClasses(); 394 void DropClasses();
395 void DropLibraries(); 395 void DropLibraries();
396 396
397 void BindStaticCalls(); 397 void BindStaticCalls();
398 void SwitchICCalls(); 398 void SwitchICCalls();
399 void ResetPrecompilerState(); 399 void ResetPrecompilerState();
400 400
401 void Obfuscate();
402
401 void CollectDynamicFunctionNames(); 403 void CollectDynamicFunctionNames();
402 404
403 void PrecompileStaticInitializers(); 405 void PrecompileStaticInitializers();
404 void PrecompileConstructors(); 406 void PrecompileConstructors();
405 407
406 void FinalizeAllClasses(); 408 void FinalizeAllClasses();
407 void VerifyJITFeedback(); 409 void VerifyJITFeedback();
408 RawScript* LookupScript(const char* uri); 410 RawScript* LookupScript(const char* uri);
409 intptr_t MapCid(intptr_t feedback_cid); 411 intptr_t MapCid(intptr_t feedback_cid);
410 412
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 } else { 471 } else {
470 ASSERT(String::Cast(obj).IsSymbol()); 472 ASSERT(String::Cast(obj).IsSymbol());
471 return String::Cast(obj).Hash(); 473 return String::Cast(obj).Hash();
472 } 474 }
473 } 475 }
474 static RawObject* NewKey(const Function& function) { return function.raw(); } 476 static RawObject* NewKey(const Function& function) { return function.raw(); }
475 }; 477 };
476 478
477 typedef UnorderedHashSet<FunctionsTraits> UniqueFunctionsSet; 479 typedef UnorderedHashSet<FunctionsTraits> UniqueFunctionsSet;
478 480
481 #if defined(DART_PRECOMPILER)
482 // ObfuscationMap maps Strings to Strings.
483 class ObfuscationMapTraits {
484 public:
485 static const char* Name() { return "ObfuscationMapTraits"; }
486 static bool ReportStats() { return false; }
487
488 // Only for non-descriptor lookup and table expansion.
489 static bool IsMatch(const Object& a, const Object& b) {
490 return a.raw() == b.raw();
491 }
492
493 static uword Hash(const Object& key) { return String::Cast(key).Hash(); }
494 };
495 typedef UnorderedHashMap<ObfuscationMapTraits> ObfuscationMap;
496
497 class Obfuscator {
498 public:
499 Obfuscator(Thread* thread, const String& private_key);
500 ~Obfuscator();
501
502 RawString* Rename(const String& name, bool atomic = false) {
503 if (state_ == NULL) {
504 return name.raw();
505 }
506
507 return state_->RenameImpl(name, atomic);
508 }
509
510 static void ObfuscateSymbolInstance(Thread* thread, const Instance& instance);
511
512 static void Deobfuscate(Thread* thread, const GrowableObjectArray& pieces);
513
514 static const char** SerializeMap(Thread* thread);
515
516 private:
517 void InitializeRenamingMap(Isolate* isolate);
518 void PreventRenaming(Dart_QualifiedFunctionName* entry_points);
519 void PreventRenaming(const char* name);
520 void PreventRenaming(const String& name) { state_->PreventRenaming(name); }
521
522 static const intptr_t kSavedStateNameIndex = 0;
523 static const intptr_t kSavedStateRenamesIndex = 1;
524 static const intptr_t kSavedStateSize = 2;
525
526 static RawArray* GetRenamesFromSavedState(const Array& saved_state) {
527 Array& renames = Array::Handle();
528 renames ^= saved_state.At(kSavedStateRenamesIndex);
529 return renames.raw();
530 }
531
532 static RawString* GetNameFromSavedState(const Array& saved_state) {
533 String& name = String::Handle();
534 name ^= saved_state.At(kSavedStateNameIndex);
535 return name.raw();
536 }
537
538 class ObfuscationState : public ZoneAllocated {
539 public:
540 ObfuscationState(Thread* thread,
541 const Array& saved_state,
542 const String& private_key)
543 : thread_(thread),
544 saved_state_(saved_state),
545 renames_(GetRenamesFromSavedState(saved_state)),
546 private_key_(private_key),
547 string_(String::Handle(thread->zone())),
548 renamed_(String::Handle(thread->zone())) {
549 memset(name_, 0, sizeof(name_));
550
551 string_ = GetNameFromSavedState(saved_state);
552 if (!string_.IsNull()) {
553 string_.ToUTF8(reinterpret_cast<uint8_t*>(name_), sizeof(name_));
554 }
555 }
556
557 void SaveState();
558
559 RawString* RenameImpl(const String& name, bool atomic);
560 RawString* BuildRename(const String& name, bool atomic);
561 RawString* NewAtomicRename(bool should_be_private);
562
563 void NextName();
564
565 void PreventRenaming(const String& name);
566 void PreventRenaming(const char* name);
567
568 private:
569 Thread* thread_;
570
571 const Array& saved_state_;
572
573 char name_[100];
574 ObfuscationMap renames_;
575
576 const String& private_key_;
577
578 String& string_;
579 String& renamed_;
580 };
581
582 ObfuscationState* state_;
583 };
584 #else
585 // Minimal do-nothing implementation of an Obfuscator for non-precompiler
586 // builds.
587 class Obfuscator {
588 public:
589 Obfuscator(Thread* thread, const String& private_key) {}
590 ~Obfuscator() {}
591
592 RawString* Rename(const String& name, bool atomic = false) {
593 return name.raw();
594 }
595
596 static void ObfuscateSymbolInstance(Thread* thread,
597 const Instance& instance) {}
598
599 static void Deobfuscate(Thread* thread, const GrowableObjectArray& pieces) {}
600 };
601 #endif // DART_PRECOMPILER
602
479 } // namespace dart 603 } // namespace dart
480 604
481 #endif // RUNTIME_VM_PRECOMPILER_H_ 605 #endif // RUNTIME_VM_PRECOMPILER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698