Skip to content

Commit

Permalink
[GR-8316] Add MitigateSpeculativeExecutionAttacks option.
Browse files Browse the repository at this point in the history
PullRequest: graal/1045
  • Loading branch information
gilles-duboscq committed Feb 20, 2018
2 parents 6345a76 + df51d06 commit 955b019
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/mx.compiler/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@
"annotationProcessors" : [
"GRAAL_NODEINFO_PROCESSOR",
"GRAAL_COMPILER_MATCH_PROCESSOR",
"GRAAL_OPTIONS_PROCESSOR",
],
"javaCompliance" : "1.8",
"workingSets" : "Graal,AMD64",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3852,4 +3852,11 @@ public void illegal() {
emitByte(0x0f);
emitByte(0x0b);
}

public void lfence() {
emitByte(0x0f);
emitByte(0xae);
emitByte(0xe8);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.graalvm.compiler.lir.amd64.AMD64ControlFlow.ReturnOp;
import org.graalvm.compiler.lir.amd64.AMD64ControlFlow.StrategySwitchOp;
import org.graalvm.compiler.lir.amd64.AMD64ControlFlow.TableSwitchOp;
import org.graalvm.compiler.lir.amd64.AMD64LFenceOp;
import org.graalvm.compiler.lir.amd64.AMD64Move;
import org.graalvm.compiler.lir.amd64.AMD64Move.CompareAndSwapOp;
import org.graalvm.compiler.lir.amd64.AMD64Move.MembarOp;
Expand Down Expand Up @@ -554,4 +555,8 @@ public SaveRegistersOp createZapRegisters(Register[] zappedRegisters, JavaConsta
public LIRInstruction createZapArgumentSpace(StackSlot[] zappedStack, JavaConstant[] zapValues) {
return new AMD64ZapStackOp(zappedStack, zapValues);
}

public void emitLFence() {
append(new AMD64LFenceOp());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

package org.graalvm.compiler.core.amd64;

import static org.graalvm.compiler.core.amd64.AMD64NodeLIRBuilder.Options.MitigateSpeculativeExecutionAttacks;

import org.graalvm.compiler.core.gen.NodeLIRBuilder;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.lir.LIRFrameState;
Expand All @@ -37,13 +39,25 @@
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.calc.IntegerDivRemNode;
import org.graalvm.compiler.nodes.calc.IntegerDivRemNode.Op;
import org.graalvm.compiler.nodes.cfg.Block;
import org.graalvm.compiler.options.Option;
import org.graalvm.compiler.options.OptionKey;
import org.graalvm.compiler.options.OptionType;
import org.graalvm.compiler.options.OptionValues;

import jdk.vm.ci.amd64.AMD64;
import jdk.vm.ci.meta.AllocatableValue;
import jdk.vm.ci.meta.Value;

public abstract class AMD64NodeLIRBuilder extends NodeLIRBuilder {

public static class Options {
// @formatter:off
@Option(help = "AMD64: Emit lfence instructions at the beginning of basic blocks", type = OptionType.Expert)
public static final OptionKey<Boolean> MitigateSpeculativeExecutionAttacks = new OptionKey<>(false);
// @formatter:on
}

public AMD64NodeLIRBuilder(StructuredGraph graph, LIRGeneratorTool gen, AMD64NodeMatchRules nodeMatchRules) {
super(graph, gen, nodeMatchRules);
}
Expand Down Expand Up @@ -121,4 +135,21 @@ protected boolean peephole(ValueNode valueNode) {
public AMD64LIRGenerator getLIRGeneratorTool() {
return (AMD64LIRGenerator) gen;
}

@Override
public void doBlockPrologue(Block block, OptionValues options) {
if (MitigateSpeculativeExecutionAttacks.getValue(options)) {
boolean hasControlSplitPredecessor = false;
for (Block b : block.getPredecessors()) {
if (b.getSuccessorCount() > 1) {
hasControlSplitPredecessor = true;
break;
}
}
boolean isStartBlock = block.getPredecessorCount() == 0;
if (hasControlSplitPredecessor || isStartBlock) {
getLIRGeneratorTool().emitLFence();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ private Value[] createPhiOut(AbstractMergeNode merge, AbstractEndNode pred) {
return values.toArray(new Value[values.size()]);
}

public void doBlockPrologue(@SuppressWarnings("unused") Block block, @SuppressWarnings("unused") OptionValues options) {

}

@Override
@SuppressWarnings("try")
public void doBlock(Block block, StructuredGraph graph, BlockMap<List<Node>> blockMap) {
Expand All @@ -341,6 +345,7 @@ public void doBlock(Block block, StructuredGraph graph, BlockMap<List<Node>> blo
}
}
}
doBlockPrologue(block, options);

List<Node> nodes = blockMap.get(block);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2018, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.graalvm.compiler.lir.amd64;

import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
import org.graalvm.compiler.lir.LIRInstructionClass;
import org.graalvm.compiler.lir.Opcode;
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;

@Opcode("LFENCE")
public final class AMD64LFenceOp extends AMD64LIRInstruction {
public static final LIRInstructionClass<AMD64LFenceOp> TYPE = LIRInstructionClass.create(AMD64LFenceOp.class);

public AMD64LFenceOp() {
super(TYPE);
}

@Override
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler asm) {
asm.lfence();
}
}

0 comments on commit 955b019

Please sign in to comment.