Skip to content

Commit 3c3bd8d

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[vm,aot,bytecode] Support obfuscation
In order to support obfuscation, string constants are decoupled from public names in bytecode (names are obfuscated, but string constants aren't). List of protected names is written in a separate section in bytecode component. Obfuscator is extended to support getter and setter names coming from bytecode. Change-Id: I8e8d820d1a8b97e32e8ad1b064b827bdb017430b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/121261 Commit-Queue: Alexander Markov <alexmarkov@google.com> Reviewed-by: Régis Crelier <regis@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
1 parent dc0db06 commit 3c3bd8d

File tree

11 files changed

+277
-75
lines changed

11 files changed

+277
-75
lines changed

pkg/vm/lib/bytecode/constant_pool.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,9 @@ class ConstantPool {
734734

735735
int addString(String value) => addObjectRef(new StringConstant(value));
736736

737+
int addName(String name) =>
738+
_add(new ConstantObjectRef(objectTable.getPublicNameHandle(name)));
739+
737740
int addArgDesc(int numArguments,
738741
{int numTypeArgs = 0, List<String> argNames = const <String>[]}) =>
739742
_add(new ConstantObjectRef(

pkg/vm/lib/bytecode/dbc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ library vm.bytecode.dbc;
1010
/// Before bumping current bytecode version format, make sure that
1111
/// all users have switched to a VM which is able to consume new
1212
/// version of bytecode.
13-
const int currentBytecodeFormatVersion = 22;
13+
const int currentBytecodeFormatVersion = 23;
1414

1515
enum Opcode {
1616
kUnusedOpcode000,

pkg/vm/lib/bytecode/declarations.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ class _Section {
11981198

11991199
class Component {
12001200
static const int magicValue = 0x44424332; // 'DBC2'
1201-
static const int numSections = 13;
1201+
static const int numSections = 14;
12021202
static const int sectionAlignment = 4;
12031203

12041204
// UInt32 magic, version, numSections x (numItems, offset)
@@ -1217,6 +1217,7 @@ class Component {
12171217
final Map<Uri, SourceFile> uriToSource = <Uri, SourceFile>{};
12181218
final List<LocalVariableTable> localVariables = <LocalVariableTable>[];
12191219
final List<AnnotationsDeclaration> annotations = <AnnotationsDeclaration>[];
1220+
Set<String> protectedNames;
12201221
ObjectHandle mainLibrary;
12211222

12221223
Component(this.version)
@@ -1229,6 +1230,13 @@ class Component {
12291230
// Write sections to their own buffers in reverse order as section may
12301231
// reference data structures from successor sections by offsets.
12311232

1233+
final protectedNamesWriter = new BufferedWriter.fromWriter(writer);
1234+
if (protectedNames != null && protectedNames.isNotEmpty) {
1235+
for (var name in protectedNames) {
1236+
protectedNamesWriter.writePackedStringReference(name);
1237+
}
1238+
}
1239+
12321240
final annotationsWriter = new BufferedWriter.fromWriter(writer);
12331241
for (var annot in annotations) {
12341242
writer.linkWriter.put(annot, annotationsWriter.offset);
@@ -1321,6 +1329,8 @@ class Component {
13211329
new _Section(lineStarts.length, lineStartsWriter),
13221330
new _Section(localVariables.length, localVariablesWriter),
13231331
new _Section(annotations.length, annotationsWriter),
1332+
new _Section(protectedNames != null ? protectedNames.length : 0,
1333+
protectedNamesWriter),
13241334
];
13251335
assert(sections.length == numSections);
13261336

@@ -1409,6 +1419,9 @@ class Component {
14091419
final annotationsNum = reader.readUInt32();
14101420
final annotationsOffset = reader.readUInt32();
14111421

1422+
final protectedNamesNum = reader.readUInt32();
1423+
final protectedNamesOffset = reader.readUInt32();
1424+
14121425
reader.offset = start + stringTableOffset;
14131426
stringTable = new StringTable.read(reader);
14141427
reader.stringReader = stringTable;
@@ -1420,6 +1433,14 @@ class Component {
14201433
// Read sections in the reverse order as section may reference
14211434
// successor sections by offsets.
14221435

1436+
if (protectedNamesNum != 0) {
1437+
protectedNames = new Set<String>();
1438+
reader.offset = start + protectedNamesOffset;
1439+
for (int i = 0; i < protectedNamesNum; ++i) {
1440+
protectedNames.add(reader.readPackedStringReference());
1441+
}
1442+
}
1443+
14231444
final annotationsStart = start + annotationsOffset;
14241445
reader.offset = annotationsStart;
14251446
for (int i = 0; i < annotationsNum; ++i) {

pkg/vm/lib/bytecode/gen_bytecode.dart

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ import '../metadata/direct_call.dart'
5959
show DirectCallMetadata, DirectCallMetadataRepository;
6060
import '../metadata/inferred_type.dart'
6161
show InferredType, InferredTypeMetadataRepository;
62+
import '../metadata/obfuscation_prohibitions.dart'
63+
show ObfuscationProhibitionsMetadataRepository;
6264
import '../metadata/procedure_attributes.dart'
6365
show ProcedureAttributesMetadata, ProcedureAttributesMetadataRepository;
6466

@@ -193,6 +195,14 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
193195

194196
inferredTypeMetadata = component
195197
.metadata[InferredTypeMetadataRepository.repositoryTag]?.mapping;
198+
199+
final obfuscationProhibitionsMetadataRepository = component
200+
.metadata[ObfuscationProhibitionsMetadataRepository.repositoryTag];
201+
if (obfuscationProhibitionsMetadataRepository != null) {
202+
bytecodeComponent.protectedNames =
203+
obfuscationProhibitionsMetadataRepository
204+
.mapping[component]?.protectedNames;
205+
}
196206
}
197207

198208
@override
@@ -249,7 +259,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
249259
source = bytecodeComponent.uriToSource[uri];
250260
if (source == null) {
251261
final importUri =
252-
objectTable.getNameHandle(null, astSource.importUri.toString());
262+
objectTable.getConstStringHandle(astSource.importUri.toString());
253263
source = new SourceFile(importUri);
254264
bytecodeComponent.sourceFiles.add(source);
255265
bytecodeComponent.uriToSource[uri] = source;
@@ -276,7 +286,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
276286
LibraryDeclaration getLibraryDeclaration(
277287
Library library, List<ClassDeclaration> classes) {
278288
final importUri =
279-
objectTable.getNameHandle(null, library.importUri.toString());
289+
objectTable.getConstStringHandle(library.importUri.toString());
280290
int flags = 0;
281291
for (var dependency in library.dependencies) {
282292
final targetLibrary = dependency.targetLibrary;
@@ -287,10 +297,10 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
287297
flags |= LibraryDeclaration.usesDartFfiFlag;
288298
}
289299
}
290-
final name = objectTable.getNameHandle(null, library.name ?? '');
300+
final name = objectTable.getPublicNameHandle(library.name ?? '');
291301
final script = getScript(library.fileUri, true);
292302
final extensionUris =
293-
objectTable.getPublicNameHandles(getNativeExtensionUris(library));
303+
objectTable.getConstStringHandles(getNativeExtensionUris(library));
294304
if (extensionUris.isNotEmpty) {
295305
flags |= LibraryDeclaration.hasExtensionsFlag;
296306
}
@@ -380,7 +390,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
380390
}
381391
}
382392

383-
final nameHandle = objectTable.getNameHandle(null, topLevelClassName);
393+
final nameHandle = objectTable.getPublicNameHandle(topLevelClassName);
384394
final script = getScript(library.fileUri, true);
385395

386396
final classDeclaration = new ClassDeclaration(
@@ -710,7 +720,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
710720
flags |= FunctionDeclaration.isExternalFlag;
711721
} else {
712722
flags |= FunctionDeclaration.isNativeFlag;
713-
nativeName = objectTable.getNameHandle(null, externalName);
723+
nativeName = objectTable.getConstStringHandle(externalName);
714724
}
715725
}
716726
int position = TreeNode.noOffset;
@@ -1825,7 +1835,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
18251835
assert(numOptionalNamed != 0);
18261836
for (int i = 0; i < numOptionalNamed; i++) {
18271837
final param = locals.sortedNamedParameters[i];
1828-
asm.emitLoadConstant(numFixed + i, cp.addString(param.name));
1838+
asm.emitLoadConstant(numFixed + i, cp.addName(param.name));
18291839
asm.emitLoadConstant(numFixed + i, _getDefaultParamConstIndex(param));
18301840
}
18311841
}
@@ -2030,7 +2040,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
20302040
isCaptured
20312041
? locals.getVarIndexInContext(variable)
20322042
: locals.getVarIndexInFrame(variable),
2033-
cp.addString(variable.name),
2043+
cp.addName(variable.name),
20342044
cp.addType(variable.type),
20352045
variable.fileOffset,
20362046
initializedPosition);
@@ -2276,7 +2286,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
22762286
_genPushInstantiatorAndFunctionTypeArguments([type, bound]);
22772287
asm.emitPushConstant(cp.addType(type));
22782288
asm.emitPushConstant(cp.addType(bound));
2279-
asm.emitPushConstant(cp.addString(typeParam.name));
2289+
asm.emitPushConstant(cp.addName(typeParam.name));
22802290
asm.emitAssertSubtype();
22812291
}
22822292

@@ -2294,11 +2304,12 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
22942304
asm.emitDrop1();
22952305
}
22962306

2297-
void _genAssertAssignable(DartType type, {String name = ''}) {
2307+
void _genAssertAssignable(DartType type, {String name, String message}) {
22982308
assert(!typeEnvironment.isTop(type));
22992309
asm.emitPushConstant(cp.addType(type));
23002310
_genPushInstantiatorAndFunctionTypeArguments([type]);
2301-
asm.emitPushConstant(cp.addString(name));
2311+
asm.emitPushConstant(
2312+
name != null ? cp.addName(name) : cp.addString(message));
23022313
bool isIntOk = typeEnvironment.isSubtypeOf(
23032314
typeEnvironment.coreTypes.intLegacyRawType,
23042315
type,
@@ -2451,11 +2462,11 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
24512462

24522463
final List<NameAndType> parameters = <NameAndType>[];
24532464
for (var v in function.positionalParameters) {
2454-
parameters.add(new NameAndType(objectTable.getNameHandle(null, v.name),
2465+
parameters.add(new NameAndType(objectTable.getPublicNameHandle(v.name),
24552466
objectTable.getHandle(v.type)));
24562467
}
24572468
for (var v in function.namedParameters) {
2458-
parameters.add(new NameAndType(objectTable.getNameHandle(null, v.name),
2469+
parameters.add(new NameAndType(objectTable.getPublicNameHandle(v.name),
24592470
objectTable.getHandle(v.type)));
24602471
}
24612472
if (function.requiredParameterCount != parameters.length) {
@@ -2475,7 +2486,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
24752486
return new ClosureDeclaration(
24762487
flags,
24772488
objectTable.getHandle(parent),
2478-
objectTable.getNameHandle(null, name),
2489+
objectTable.getPublicNameHandle(name),
24792490
position,
24802491
endPosition,
24812492
typeParams,
@@ -2758,7 +2769,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
27582769
_genPushReceiver();
27592770

27602771
// Argument 0 for _allocateInvocationMirror(): function name.
2761-
asm.emitPushConstant(cp.addString(name));
2772+
asm.emitPushConstant(cp.addName(name));
27622773

27632774
// Argument 1 for _allocateInvocationMirror(): arguments descriptor.
27642775
asm.emitPushConstant(argDescCpIndex);
@@ -2791,7 +2802,8 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
27912802
return;
27922803
}
27932804

2794-
_genAssertAssignable(type, name: node.isTypeError ? '' : symbolForTypeCast);
2805+
_genAssertAssignable(type,
2806+
message: node.isTypeError ? '' : symbolForTypeCast);
27952807
}
27962808

27972809
@override

0 commit comments

Comments
 (0)